C++文件打开

来源:互联网 发布:网络推广岗位说明书 编辑:程序博客网 时间:2024/05/01 03:23

打开文件,如果文件不存在不新建

#include <fstream.h>

#include <stdlib.h> //exit()

int main()

{

ifstream infile;

infile.open("myfile.dat",ios::nocreate);

if(infile.fail())

{

cout << "the file are notsuccessfully opened"<<endll;

exit(1);

}

cout<<"the file has beensuccessfully opened for reading";

return 1;

}

任何被打开的文件在程序结束后都会自动被操作系统关闭。

关闭文件可以使用infile.close();

可以使用符号 <<

ofstream outfile;

outfile.open(filename);

outfile << "hello world";

 

原创粉丝点击