How to edit file txt already created in C++

最后都变了- 提交于 2019-12-10 11:38:34

问题


I have know how to create export data to file txt,but If I already txt file, how to edit that file txt which don't apply to data have already exist.. This also mean add a new data line in a txt file already have data..


回答1:


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




回答2:


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
}


来源:https://stackoverflow.com/questions/16923488/how-to-edit-file-txt-already-created-in-c

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