2016-05-17 482 views
0

使用wfstream我想寫一些處理內容從文件中刪除舊內容。寫新內容刪除舊內容

wfstream cSrcFileOutput(m_cstrFile); 
wfstream cSrcFileInput(m_cstrFile); 
std::wstring cstrSrcFileContent; 
.... 

cstrSrcFileContent有我的內容

我下面寫:

cSrcFileOutput.write(cstrSrcFileContent.c_str(), cstrSrcFileContent.size()*sizeof(wchar_t)); 

的問題是它不清除以前的數據。相反,它會在文件的某個位置插入已處理的內容。

我想舊的內容替換爲新的處理內容。

請建議。

+1

[清除C++中的文本文件中的數據]的可能重複(http://stackoverflow.com/questions/17032970/clear-data-inside-text-file-in-c) –

回答

0

當您打開文件,您可以設置如何將數據應該通過第二個參數寫入到文件的構造

std::wfstream cSrcFileOutput(m_cstrFile, std::wfstream::out | std::wfstream::trunc); 

其中的std :: wfstream :: TRUNC指覆蓋現有的內容模式(有關構造函數文檔,請參見std::basic_fstream::basic_fstream)。