要避免清除txt文件原有内容被覆盖掉,可以使用ofstream来打开文件并写入内容。而不是使用fputs函数。

示例代码:

#include <iostream>
#include <fstream>

int main() {
    std::ofstream file("example.txt", std::ios::app); // 以追加模式打开文件
    if (file.is_open()) {
        std::string text = "Hello, World!";
        file << text; // 写入内容
        file.close(); // 关闭文件
    } else {
        std::cout << "无法打开文件" << std::endl;
    }

    return 0;
}

这样,每次运行程序时,新的内容都会被追加到文件末尾,而不会覆盖掉原有内容。

stdstring 变量在clear之后再用fputs函数写入txt文件txt文件原有内容会被覆盖掉如何处理

原文地址: http://www.cveoy.top/t/topic/ilEb 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录