简体   繁体   中英

CStdioFile problems with encoding on read file

I can't read a file correctly using CStdioFile.

I open notepad.exe, I type àèìòùáéíóú and I save twice, once I set codification as ANSI (really is CP-1252 ) and other as UTF-8 .

Then I try to read it from MFC with the following block of code

BOOL ReadAllFileContent(const CString &FilePath, CString *fileContent)
{
    CString sLine;
    BOOL isSuccess = false;

    CStdioFile input;
    isSuccess = input.Open(FilePath, CFile::modeRead);
    if (isSuccess) {
        while (input.ReadString(sLine)) {
            fileContent->Append(sLine);
        }
        input.Close();
    }
    return isSuccess;
}

When I call it, with ANSI file I've got the expected result àèìòùáéíóú but when I try to read the UTF8 encoded file I've got à èìòùáéÃóú

I would like my function works with all files regardless of the encoding.

Why I need to implement?

.EDIT.

  • Unfortunately, in the real app, files come from external app so change the file encoding isn't an option.I must be able to read both UTF-8 and CP-1252 files.
  • Any file is valid ANSI , what notepad told ANSI is really Windows-1252 encode.
  • I've figured out a way to read UTF-8 and CP-1252 right based on the example provided here . Although it works, I need to pass the file encode which I don't know in advance.

Thnks!

I personally use the class as advertised here:

https://www.codeproject.com/Articles/7958/CTextFileDocument

It has excellent support for reading and writing text files of various encodings including unicode in its various flavours.

I have not had a problem with it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM