2016-05-17 475 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)。