C++ 向txt文件里面追加文本信息

zy18730608243 2016-05-11 09:16:20
c++如何实现:
向一个txt文件里面多次追加文字信息,第一次追加的文本信息不会把原txt里面的信息覆盖,第二次追加信息把第一次追加进去的信息覆盖,第三次追加的信息把第二次追加的信息覆盖。依次,后面追加的内容把前一次追加的内容覆盖。
...全文
803 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
dongfangbuba 2016-05-12
  • 打赏
  • 举报
回复
引用 3 楼 x20121223 的回复:
[quote=引用 2 楼 zy18730608243 的回复:] [quote=引用 1 楼 dustpg 的回复:] 不就是只保留第一次与最后一次么,说得这么麻烦,记录第一次结尾位置不就行了
在写入txt之前,原txt内容要保留,并且追加进去的内容只保留最后一次追加的。。这个怎么实现呢?[/quote] 这你还不如把要写入的信息先缓存在一个buffer中,然后在程序结束位置再将其写入txt。[/quote] zhege hao aaa
纹枰老妖 2016-05-12
  • 打赏
  • 举报
回复
建议搜索ios::app,该标志说明程序仅能打开一个只能用于输入的文件,即只能追加内容
赵4老师 2016-05-12
  • 打赏
  • 举报
回复
不要迷信书、考题、老师、回帖; 要迷信CPU、编译器、调试器、运行结果。 并请结合“盲人摸太阳”和“驾船出海时一定只带一个指南针。”加以理解。 任何理论、权威、传说、真理、标准、解释、想象、知识……都比不上摆在眼前的事实! 有人说一套做一套,你相信他说的还是相信他做的? 其实严格来说这个世界上古往今来所有人都是说一套做一套,不是吗? 不要写连自己也预测不了结果的代码!
赵4老师 2016-05-12
  • 打赏
  • 举报
回复
Reference <fstream> ofstream open public member function <fstream> std::ofstream::openvoid open (const char* filename, ios_base::openmode mode = ios_base::out); void open (const char* filename, ios_base::openmode mode = ios_base::out); void open (const string& filename, ios_base::openmode mode = ios_base::out); Open file Opens the file identified by argument filename, associating it with the stream object, so that input/output operations are performed on its content. Argument mode specifies the opening mode. If the stream is already associated with a file (i.e., it is already open), calling this function fails. The file association of a stream is kept by its internal stream buffer: Internally, the function calls rdbuf()->open(filename,mode|ios_base::out) The function sets failbit in case of failure. The function clears the stream's state flags on success (setting them to goodbit). In case of failure, failbit is set. Parameters filename String with the name of the file to open. Specifics about its format and validity depend on the library implementation and running environment. mode Flags describing the requested input/output mode for the file. This is an object of the bitmask member type openmode that consists of a combination of the following member constants: member constant stands for access in input File open for reading: the internal stream buffer supports input operations. out * output File open for writing: the internal stream buffer supports output operations. binary binary Operations are performed in binary mode rather than text. ate at end The output position starts at the end of the file. app append All output operations happen at the end of the file, appending to its existing contents. trunc truncate Any contents that existed in the file before it is open are discarded. These flags can be combined with the bitwise OR operator (|). * out is always set for ofstream objects (even if explicitly not set in argument mode). Note that even though ofstream is an output stream, its internal filebuf object may be set to also support input operations. If the mode has both trunc and app set, the opening operation fails. It also fails if both app and in are set simultaneously. If the mode has both trunc and app set, the opening operation fails. Return Value none If the function fails to open a file, the failbit state flag is set for the stream (which may throw ios_base::failure if that state flag was registered using member exceptions). Example 1234567891011121314 // ofstream::open / ofstream::close #include <fstream> // std::ofstream int main () { std::ofstream ofs; ofs.open ("test.txt", std::ofstream::out | std::ofstream::app); ofs << " more lorem ipsum"; ofs.close(); return 0; } Data races Modifies the ofstream object. Concurrent access to the same stream object introduce data races. Exception safety Basic guarantee: if an exception is thrown, the stream is in a valid state. It throws an exception of member type failure if the function fails (setting the failbit state flag) and member exceptions was set to throw for that state. See also ofstream::is_openCheck if file is open (public member function )ofstream::closeClose file (public member function )filebuf::openOpen file (public member function )
oiq12 2016-05-12
  • 打赏
  • 举报
回复
同一楼。。记录结尾位置不就可以了。
wersdfadaf 2016-05-11
  • 打赏
  • 举报
回复
引用 2 楼 zy18730608243 的回复:
[quote=引用 1 楼 dustpg 的回复:] 不就是只保留第一次与最后一次么,说得这么麻烦,记录第一次结尾位置不就行了
在写入txt之前,原txt内容要保留,并且追加进去的内容只保留最后一次追加的。。这个怎么实现呢?[/quote] 这你还不如把要写入的信息先缓存在一个buffer中,然后在程序结束位置再将其写入txt。
zy18730608243 2016-05-11
  • 打赏
  • 举报
回复
引用 1 楼 dustpg 的回复:
不就是只保留第一次与最后一次么,说得这么麻烦,记录第一次结尾位置不就行了
在写入txt之前,原txt内容要保留,并且追加进去的内容只保留最后一次追加的。。这个怎么实现呢?
dustpg 2016-05-11
  • 打赏
  • 举报
回复
不就是只保留第一次与最后一次么,说得这么麻烦,记录第一次结尾位置不就行了

64,676

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧