在C ++中添加一个换行符 [英] adding a newline to file in C++

查看:193
本文介绍了在C ++中添加一个换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



以下是我的代码:

 #包括<&的iostream GT; 
#include< fstream>
使用namespace std;
int main()
{
ofstream savefile(anish.txt);
savefile<<<这是第一个编程程序<<\\\
这是一个实验。
savefile.close();
返回0;





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

我有:



<这是第一个程序,我作家这是一个实验



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



这是第一个程序

我是作者,这是一个实验


我应该怎么做格式化输出这样的方式吗?

解决方案

首先,您需要打开流才能写入文件。 b $ b

  ofstream file; // out file stream 
file.open(anish.txt);

之后,您可以使用 << 运算符。

 文件<< 这是我的第一个节目作家; 

还可以使用 std :: endl \\\
。例如:

  file<< 这是第一个我编程的程序<< endl<< 这是一个实验; 


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 ?

解决方案

First you need to open stream to write to a file.

ofstream file; // out file stream
file.open("anish.txt");

After that you can read to file using << operator.

file << "hi this is first program i writer";

Also use std::endl instead of \n. For instance:

file << "hi this is first program i writer" << endl << "this is an experiment";

这篇关于在C ++中添加一个换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆