打开文件,如果文件不存在不新建
#include <fstream.h>
#include <stdlib.h> //exit()
int main()
{
ifstream infile;
infile.open("myfile.dat",ios::nocreate);
if(infile.fail())
{
cout << "the file are not successfully opened"<<endll;
exit(1);
}
cout<<"the file has been successfully opened for reading";
return 1;
}
任何被打开的文件在程序结束后都会自动被操作系统关闭。
关闭文件可以使用infile.close();
可以使用符号 <<
ofstream outfile;
outfile.open(filename);
outfile << "hello world";