Handling Non-Ascii Chars in C++

前端 未结 2 740
予麋鹿
予麋鹿 2021-01-01 04:40

I am facing some issues with non-Ascii chars in C++. I have one file containg non-ascii chars which I am reading in C++ via file Handling. After reading the file(say 1.txt)

2条回答
  •  情深已故
    2021-01-01 05:44

    Sounds to me like a utf8 issue. Since you didn't tag your question with c++11 Here Is an excelent article on unicode and c++ streams.

    From your updated code, let me explain what is happening. You create a file stream to read your file. Internally the file stream only recognizes chars, until you tell it otherwise. A char, on most machines, can only hold 8 bits of data, but the characters in your file are using more than 8 bits. To be able to read your file correctly, you NEED to know how it is encoded. The most common encoding is UTF-8, which uses between 1 and 4 chars for each character.

    Once you know your encoding, you can either use wifstream (for UTF-16) or imbue() a locale for other encodings.

    Update: If your file is ISO-88591 (from your comment above), try this.

    wifstream myReadFile;
    myReadFile.imbue(std::locale("en_US.iso88591"));
    myReadFile.open("11.txt");
    

提交回复
热议问题