C語言文件寫入

1.文件指針的方式

頭文件

#include<conio.h>
#include<stdlib.h>
#inlcude<fstream>

聲明變量

FILE * fp;

查看文件創建是否成功

if((fp = fopen("test.dat","w")) == NULL)
{
    printf("\nerror on open test.dat!");
    getch();
    exit(1);
}

寫入數據

fprintf(fp,"%lf",t);    //假如t是個double類型的變量

2.輸入輸出流的方式

頭文件

#include<iostream>
#include<stdlib.h>
using namespace std;

創建輸出流

ofstream A;
A.open("test.dat",ios::out);

判斷輸出流是否創建成功

if(!A)
{
    cout<<"can't opne file"<<endl;
    exit(1);
}

輸出數據

A<<"t的值wei:"<<t<<endl;    //類似於cout

關閉流

A.close();

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章