【问题标题】:write new content removing old contents编写新内容删除旧内容
【发布时间】:2016-05-17 10:16:02
【问题描述】:

使用 wfstream 我正在尝试编写一些已处理的内容,从文件中删除旧内容。

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

cstrSrcFileContent 有我的内容

我写如下:

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

问题是它没有清除以前的数据。而是在文件中的某处插入处理过的内容。

我想用新处理的内容替换旧内容。

请提出建议。

【问题讨论】:

标签: c++ c++11 mfc


【解决方案1】:

打开文件时,通过构造函数的第二个参数设置数据应该如何写入文件的模式

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

其中 std::wfstream::trunc 表示覆盖现有内容(有关构造函数文档,请参阅std::basic_fstream::basic_fstream)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-14
    • 2023-03-10
    • 1970-01-01
    • 2011-10-03
    • 2013-05-22
    相关资源
    最近更新 更多