如何防止用 ofstream 覆盖文本文件中的信息
How do I prevent overwriting information in a text file with the ofstream?
我目前正在学习C++,我试图将文本添加到文件中,但它一直覆盖它,无论如何我可以将东西附加到它吗?
#include <iostream>
#include <cstdlib>
#include <fstream>
int main()
{
std::ofstream fil3;
fil3.open("test.txt");
file3 << " bye!"; // replaces text already existing in file :(
fil3.close();
return 0;
}
所以,我已经有一个带有"好"字样的文本文件,我想尝试添加"再见!",所以我有"再见!有人可以向我解释我应该在这里使用什么功能吗?谢谢!
打开文件时,请使用追加标志:
file3.open("test.txt", std::ofstream::app);
您可以在此处阅读有关文件操作选项的更多信息。
相关文章:
- 如何修复函数中的 fstream 文件输入以将正确的信息存储在结构数组中?
- 创建进行生产构建并创建调试信息的C++生成文件的最佳方法?
- 将对象信息保存到二进制文件中
- 错误 VSP1011:无法获取调试信息.与 /配置文件链接器开关链接
- Qt读取文本文件的一些信息
- 将文件信息(字符数组)嵌入到编译的二进制文件中
- 从带有 getline() 的文件读入一行上有多个信息得到无效参数错误
- 如何使用所有其他文件信息来编译代码,例如HAAR级联训练的权重的XML文件
- 是否可以将信息从XML文件转换为CPP
- 如何在文本文件中搜索员工记录(按名称)并仅显示其详细信息?
- CVE解析文件信息
- C++Pig拉丁代码,只删除文件信息
- 如何在Linux终端的目录下显示详细的文件信息?-C++
- CaptureStackBackTrace和文件信息
- 如何在 Linux 中获取当前目录下的文件信息
- 如何获取文件信息
- 损坏的配置文件信息与g++和CMake
- fscanf,逗号文本文件信息字符串和数字
- 添加资源.Rc文件信息项目
- 在用c++或c#打开文件之前检测文件信息