使用 CFile::typeUnicode

Por*_*Man 2 unicode mfc

我需要能够读取/写入包含中文字符的文件 unicode 字符串。

文档说 CFile::typeUnicode “仅在派生类中使用”,但我找不到对使用它的任何派生类的任何引用。

是否有任何“官方”版本的 CFile 允许我读取和写入 unicode?

或者我最好尝试使用这样的东西:http://www.codeproject.com/Articles/4119/CStdioFile-driven-class-for-multibyte-and-Unicode

Edw*_*nts 5

这似乎是一种更简单的方法:请参阅如何通过 CStdioFile 以 Unicode 读取和写入文本文件,它使用FILE流以 Unicode 打开文件,然后CStdioFile使用该流打开一个类。

//
// For Writing
//

// Old-Style... do not use...
//CStdioFile f;
//f.Open(_T("\test.txt"), CFile::modeCreate | CFile::modeWrite);

// Open the file with the specified encoding
FILE *fStream;
errno_t e = _tfopen_s(&fStream, _T("\test.txt"), _T("wt,ccs=UNICODE"));
if (e != 0) return; // failed..
CStdioFile f(fStream);  // open the file from this stream

f.WriteString(_T("Test"));
f.Close();

//
// For Reading
//

// Open the file with the specified encoding
FILE *fStream;
errno_t e = _tfopen_s(&fStream, _T("\test.txt"), _T("rt,ccs=UNICODE"));
if (e != 0) return; // failed..CString sRead;
CStdioFile f(fStream);  // open the file from this stream
CString sRead;
f.ReadString(sRead);
f.Close();
Run Code Online (Sandbox Code Playgroud)