简体   繁体   English

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

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

Can any body help me with this simple thing in file handling?任何机构都可以帮助我处理文件处理中的这个简单事情吗?

The following is my code:以下是我的代码:

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

It is running successfully now, I want to format the output of the text file according to my way.现在运行成功了,我想按照我的方式格式化文本文件的输出。

I have:我有:

hi this is first program i writer this is an experiment嗨,这是我编写的第一个程序,这是一个实验

How can I make my output file look like the following:我怎样才能让我的输出文件看起来像下面这样:

hi this is first program嗨,这是第一个程序

I writer this is an experiment我写这是一个实验

What should I do to format the output in that way?我应该怎么做才能以这种方式格式化输出?

Can any body help me with this simple thing in file handling?任何机构都可以帮助我处理文件处理中的这个简单问题吗?

The following is my code:以下是我的代码:

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

It is running successfully now, I want to format the output of the text file according to my way.现在运行成功,我想按照我的方式格式化文本文件的输出。

I have:我有:

hi this is first program i writer this is an experiment嗨,这是我写的第一个程序,这是一个实验

How can I make my output file look like the following:如何使我的输出文件如下所示:

hi this is first program嗨,这是第一个程序

I writer this is an experiment我写这是一个实验

What should I do to format the output in that way ?我应该怎么做才能以这种方式格式化输出?

Can any body help me with this simple thing in file handling?任何机构都可以帮助我处理文件处理中的这个简单问题吗?

The following is my code:以下是我的代码:

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

It is running successfully now, I want to format the output of the text file according to my way.现在运行成功,我想按照我的方式格式化文本文件的输出。

I have:我有:

hi this is first program i writer this is an experiment嗨,这是我写的第一个程序,这是一个实验

How can I make my output file look like the following:如何使我的输出文件如下所示:

hi this is first program嗨,这是第一个程序

I writer this is an experiment我写这是一个实验

What should I do to format the output in that way ?我应该怎么做才能以这种方式格式化输出?

Can any body help me with this simple thing in file handling?任何机构都可以帮助我处理文件处理中的这个简单问题吗?

The following is my code:以下是我的代码:

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

It is running successfully now, I want to format the output of the text file according to my way.现在运行成功,我想按照我的方式格式化文本文件的输出。

I have:我有:

hi this is first program i writer this is an experiment嗨,这是我写的第一个程序,这是一个实验

How can I make my output file look like the following:如何使我的输出文件如下所示:

hi this is first program嗨,这是第一个程序

I writer this is an experiment我写这是一个实验

What should I do to format the output in that way ?我应该怎么做才能以这种方式格式化输出?

use ios_base::app instead (adding lines instead of overwriting):使用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