使用的CFile :: typeUnicode(Using CFile::typeUnicode)

2019-10-18 07:10发布

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

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

有CFile的任何“官方”版本,这将让我读,写的unicode?

还是我最好尝试使用这样的: http://www.codeproject.com/Articles/4119/CStdioFile-derived-class-for-multibyte-and-Unicode

Answer 1:

这似乎是一个简单的方法:看如何阅读,并通过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();


Answer 2:

你也可以考虑使用不同的类,它所有的辛勤工作的你。 我用CTextFileDocument:

CTextFileDocument类帮助主题

您可以使用所提供的CTextFileWrite写了一些Unicode的味道。 例:

//Create file. Use UTF-8 to encode the file
CTextFileWrite myfile(_T("samplefile.txt"), 
            CTextFileWrite::UTF_8 );

ASSERT(myfile.IsOpen());

//Write some text
myfile << "Using 8 bit characters as input";
myfile.WriteEndl();
myfile << L"Using 16-bit characters. The following character is alfa: \x03b1";
myfile.WriteEndl();
CString temp = _T("Using CString.");
myfile << temp;


文章来源: Using CFile::typeUnicode
标签: unicode mfc