问题
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