在调试程序的时候,有一些关键的信息,需要写在文件中,以便于对代码执行过程进行检查,并对参数进行调整,所以在这里记录下C++写文件的代码。
#include<fstream>
#include<iostream>
using namespace std;
int main()
{
ofstream OutFile("Test.txt");
OutFile << "This is a Test12!"<<endl;
OutFile << "This is a Test23!"<<endl;
OutFile.close();
OutFile.open("Test.txt");
OutFile << "This is a Test34!" << endl;
OutFile << "This is a Test56!" << endl;
ofstream OutFile1("Test1.txt",ios::app);
OutFile1 << "This is a Test12!"<<endl;
OutFile1 << "This is a Test23!"<<endl;
OutFile1.close();
OutFile1.open("Test1.txt",ios::app);
OutFile1 << "This is a Test34!"<<endl;
OutFile1 << "This is a Test56!"<<endl;
OutFile1.close();
return 0;
}
看一下运行结果:
Test.txt
Test1.txt
ofstream流,以ios::app打开(或者“ios::app|ios::out”),如果没有文件,那么生成空文件;如果有文件,那么在文件尾追加。
而如果以默认的方式打开,那么每次都会在文件的开始来写,会覆盖掉原有的内容。
更多的内容,请参考:
博客一:https://blog.csdn.net/cabbage2008/article/details/53307409
博客二:https://blog.csdn.net/weixin_42333471/article/details/88093789
646




被折叠的 条评论
为什么被折叠?



