我需要能够读/写包含中文字符的unicode字符串。
文档说CFile::typeUnicode“仅在派生类中使用”,但我找不到任何使用它的派生类的引用。
有没有什么“官方”版本的CFile可以让我读写unicode?
或者我尝试使用这样的东西会更好:http://www.codeproject.com/Articles/4119/CStdioFile-derived-class-for-multibyte-and-Unicode
发布于 2013-06-29 16:05:09
这似乎是一种更简单的方法:参见How to Read and Write Text Files in Unicode through CStdioFile,它使用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();发布于 2016-03-31 03:46:52
您还可以考虑使用一个不同的类来为您完成所有繁重的工作。我使用CTextFileDocument
CTextFileDocument class help topic
您可以使用提供的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;https://stackoverflow.com/questions/17374330
复制相似问题