How to edit file txt already created in C++

元气小坏坏 提交于 2019-12-08 21:42:48

What you're looking for is std::ios_base::app which will append what you write to the file at the end.

You can use 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
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!