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();