//打开文件向文件中写入数据
#include <fstream.h>
#include <iostream>#include <stdlib.h>
using namespace std;
int main()
{
int a=20;
ofstream outfile;
outfile.open("hu.txt"); //hu.txt是存放数据的文件名
if(outfile.is_open())
{
outfile<<a<<endl; //a是程序中处理的数据
outfile.close();
}
else
{ cout<<"不能打开文件!"<<endl;}
return 0;
}
////打开文件向文件中写入数据
#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;
int main()
{
fstream outfile;
outfile.open("in.txt",ios::out);
if(!outfile)
{
cout<<"in.txt can't open!\n";
abort();
}
outfile<<"这是一个程序。\n"<<"这是一个字符串。\nOK";
outfile.close();
system("pause");
}