在文件中写入新行 - C ++ [英] Write new lines in a file - C++

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

问题描述

我试图在文件的末尾插入新行,这样它就不会更新文件,只是在最后插入新行而不改变文件中的任何内容。

我的代码是这里。

i tried to insert new line at the end of the file so that it will not update the file but just insert new line at the end without altering whatever is in the file.
my code is here.

#include<iostream>
#include<fstream>
#include<sstream>
using namespace std;
 
int main()
{
	ofstream file("outputcheck.txt");
	ostringstream oss;
	string name="we most";
	string n="people";
	file<<name << "loves" <<n;
	return 0;
}





我的尝试:



它给出了一个包含该内容的文件,但每次都会更新文件,我的意思是文件中的任何内容,只写上面一行。可以有人帮助我。



What I have tried:

it gives a file with that content but each time file will be updated, i mean whatever in file it goes and write just the above line. can somebody help me out.

推荐答案

ofstream构造函数有两个参数。第二个参数指定打开模式,默认为out。将其更改为'app'代表追加。
The ofstream constructor has two parameters. The second parameter specifies the open mode and it defaults to 'out'. Change it to 'app' which stands for append.


请参阅我对该问题的评论。您甚至没有尝试插入或添加新行。



C ++面向对象<<流操作员太不方便了!但也许你必须学会​​如何使用它们。在这种流编程风格中,有 endl 操纵器

endl - C ++参考 [ ^ ]。



或者,您可以在其中一个字符串中使用\ n,如下所示:< a href =http://www.cplusplus.com/doc/tutorial/basic_io>基本输入/输出 - C ++教程 [ ^ ]。



现在,有一个非常不愉快的问题:线路文件和字符串中使用的分隔符与平台有关。本文提供了一个非常全面的问题概述: Newline - Wikipedia,免费的百科全书 [ ^ ]。



有编程提供多平台设施以使用此分隔符的系统,就是它,它们在不同的OS上返回不同的字符串,然后添加/返回此字符串,这在代码中没有静态定义。对于C ++,这种方法并不典型:如果你写\ n,这总是一个代码点为10的字符,而\ r - 13就是它。



另一方面,越来越多的软件产品被开发出来以容忍各种行分隔符,它们规范化了。比如,如果您按原样读取文件并将结果字符串提供给Windows MessageBox ,则消息将以相同的方式显示,即使您使用其他一些行分隔符操作系统。



-SA
Please see my comments to the question. You did not even try to insert or add a new line.

C++ "object-oriented" << stream operators are so inconvenient! But perhaps you have to learn how to work with them. In this style of stream programming, there is and endl manipulator:
endl — C++ Reference[^].

Alternatively, you can simply use "\n" inside one of your strings, as shown here: Basic Input/Output - C++ Tutorials[^].

Now, there is one extremely unpleasant problem: the line separators used in files and strings are platform-dependent. This article provides a pretty comprehensive overview of the problem: Newline — Wikipedia, the free encyclopedia[^].

There are programming systems providing multiplatform facility for using this separator, that's it, they return different strings on different OS, and then you add/return this string, which is not statically defined in your code. For C++, such approach is not typical: if you write "\n", this is always a character with code point 10, and "\r" — 13, that's it.

From the other hand, more and more software products are developed to be tolerant to the kinds of line separator, they "normalize" the. Say, if you read the file as is and feed resulting string to a Windows MessageBox, the message would be shown the same way, even if you use a line separator from some other OS.

—SA


这篇关于在文件中写入新行 - C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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