c++ - 在 C++ 中向文件添加换行符

标签 c++ file-io

谁能帮我解决这个简单的文件处理问题?

以下是我的代码:

#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/

相关文章:

c++ - 为什么没有 set_union 等使用容器类型而不是迭代器的便利函数?

java - 实现类对象和Arraylist

c - 在C中写入文件多线程

python - 查找操作的基数是否会影响其运行速度,或者只是计算某些标准偏移量的方式?

c++ - 如何在c/c++中将文件从一个目录复制到另一个目录

c++ - 2 "interconnected"C++ 类的内存管理

c++ - 将clock_gettime移植到windows

c++ - 带有枚举的结构的 C++ 复制构造函数的签名

java - 如何在 Java/Scala 中正确地将字节流保存到文件中?如何修复错误保存的流?

c++ - 如何在 C++ 中求解五次多项式