谁能帮我解决这个简单的文件处理问题?
以下是我的代码:
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ofstream savefile("anish.txt");
savefile<<"hi this is first program i writer" <<"\n this is an experiment";
savefile.close();
return 0 ;
}
现在运行成功了,我想按照我的方式格式化文本文件的输出。
我有:
hi this is first program i writer this is an experiment
如何使我的输出文件如下所示:
hi this is first program
I writer this is an experiment
我应该怎么做才能以这种方式格式化输出?
最佳答案
#include <fstream>
using namespace std;
int main(){
fstream file;
file.open("source\\file.ext",ios::out|ios::binary);
file << "Line 1 goes here \n\n line 2 goes here";
// or
file << "Line 1";
file << endl << endl;
file << "Line 2";
file.close();
}
同样,希望这就是您想要的 =)
关于c++ - 在 C++ 中向文件添加换行符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5373766/