【问题标题】:writing to a file mfc写入文件 mfc
【发布时间】:2012-06-06 13:33:31
【问题描述】:

如何使用 MFC 将 unicode 写入文件。我尝试使用 Cfile 类写入文件,但不确定如何指定 TCHAR 的长度,也无法打印出新行。欢迎使用代码 sn-p。

【问题讨论】:

  • Google 搜索:“mfc 写入文件”提供了大量示例
  • length_of_string * sizeof(TCHAR)?
  • 您不应该使用 TCHAR,因为这样文件的实际内容将取决于编译设置(因此,只有您的程序才能使用该文件 - 这可能不是您想要的) .请参考 utf8everywhere.org 获取正确的独立于平台的文件内容。

标签: c++ file unicode mfc


【解决方案1】:

如果您想将 \n 自动转换为 \r\n 字符,请使用 CStdioFile 和方法 ReadString/WriteString。以下示例使用 CFile:

CString strFileContent;
CString strFilePath; 

CFile theFile(strFilePath, CFile::modeReadWrite | CFile::modeCreate);

// write BOM if you like
// WCHAR bom = 0xFEFF;
// theFile.Write(&bom, 2);

theFile.Write( (LPCTSTR) strFileContent, 
    strFileContent.GetLength() * sizeof(TCHAR));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-11
    • 1970-01-01
    • 2015-01-05
    相关资源
    最近更新 更多