2013-06-04 32 views
1

我知道如何創建數據導出到文件TXT,但如果我已經txt文件,如何編輯它並不適用於數據已經存在該文件TXT .. 已經創建的文件TXT這也意味着在一個txt文件中添加一個新的數據行已經有了數據..如何編輯C++

+0

你能告訴我們一些代碼,更清楚你真正想要的「編輯」什麼解釋了一下。 –

+4

打開追加模式,附加文字,將其關閉。 –

回答

1

可以使用的fstream(#包括<的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 
}