1

我知道如何创建导出数据到文件 txt,但是如果我已经是 txt 文件,如何编辑那个不适用于数据的文件 txt 已经存在。这也意味着已经在 txt 文件中添加了一个新的数据行有资料。。

4

2 回答 2

2

您可以使用 fstream (#include < fstream >):

// declare variable "file"
std::fstream file;
// open file named "data.txt" for writing (std::fstream::app lets you add text to the end of the file)
file.open("data.txt", std::fstream::in | std::fstream::out | std::fstream::app);
// could not open file
if(!file.is_open()) {
    // do something, e.g. print error message: std::cout << "Couldn't open file." << endl;
// file is open, you can write to it
} else {
    file << "\n"; // add a new line to the file
    file.close(); // close file
}
于 2013-06-04T17:29:12.660 回答
2

您正在寻找的是std::ios_base::app将您写入文件的内容附加到最后。

于 2013-06-04T17:05:24.313 回答