繁体   English   中英

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

[英]adding a newline to file in C++

任何机构都可以帮助我处理文件处理中的这个简单事情吗?

以下是我的代码:

#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 ;
 }

现在运行成功了,我想按照我的方式格式化文本文件的输出。

我有:

嗨,这是我编写的第一个程序,这是一个实验

我怎样才能让我的输出文件看起来像下面这样:

嗨,这是第一个程序

我写这是一个实验

我应该怎么做才能以这种方式格式化输出?

任何机构都可以帮助我处理文件处理中的这个简单问题吗?

以下是我的代码:

#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 ;
 }

现在运行成功,我想按照我的方式格式化文本文件的输出。

我有:

嗨,这是我写的第一个程序,这是一个实验

如何使我的输出文件如下所示:

嗨,这是第一个程序

我写这是一个实验

我应该怎么做才能以这种方式格式化输出?

任何机构都可以帮助我处理文件处理中的这个简单问题吗?

以下是我的代码:

#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 ;
 }

现在运行成功,我想按照我的方式格式化文本文件的输出。

我有:

嗨,这是我写的第一个程序,这是一个实验

如何使我的输出文件如下所示:

嗨,这是第一个程序

我写这是一个实验

我应该怎么做才能以这种方式格式化输出?

任何机构都可以帮助我处理文件处理中的这个简单问题吗?

以下是我的代码:

#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 ;
 }

现在运行成功,我想按照我的方式格式化文本文件的输出。

我有:

嗨,这是我写的第一个程序,这是一个实验

如何使我的输出文件如下所示:

嗨,这是第一个程序

我写这是一个实验

我应该怎么做才能以这种方式格式化输出?

使用ios_base::app代替(添加行而不是覆盖):

#include<fstream>

using namespace std;

int main()
{
    ofstream savefile("anish.txt", ios_base::app);
    savefile<<"hi this is first program i writer" <<"\n this is an experiment";
    savefile.close();
    return 0 ;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM