【问题标题】:Using CFile::typeUnicode使用 CFile::typeUnicode
【发布时间】:2013-06-26 19:04:51
【问题描述】:

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

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

是否有任何“官方”版本的 CFile 可以让我读写 unicode?​​p>

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

【问题讨论】:

    标签: unicode mfc


    【解决方案1】:

    这似乎是一种更简单的方法:参见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();
    

    【讨论】:

      【解决方案2】:

      您还可以考虑使用其他课程来为您完成所有艰苦的工作。我使用 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;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-02-12
        • 1970-01-01
        • 1970-01-01
        • 2016-04-25
        • 2011-01-20
        • 1970-01-01
        • 2014-10-24
        相关资源
        最近更新 更多