简体   繁体   English

打开并写入现有的fstream而不用C ++中的覆盖/清除

[英]open and write to existing fstream without overwrite/clearing in C++

I have an existing txt file, I want to be able to write stuff to it (on the next line) without modifying the existing contents. 我有一个现有的txt文件,我希望能够在不修改现有内容的情况下向它写入内容(在下一行)。 What's a good way to do this? 这样做的好方法是什么?

#include <iostream>
ofstream myfile ("ex.txt"); 

seems to clear all the existing text. 似乎清除了所有现有文本。

ofstream myfile ("ex.txt", ios_base::app); will open a file in append mode. 将以追加模式打开文件。 Each write operation will append to the existing contents. 每个写操作都将附加到现有内容。

使用ios_base::app标志打开文件

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

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