Click here to Skip to main content
15,914,225 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
currently reading a text file using Unicode and ANSI are supported using TChar but how to read a text file using UTF-8,ANSI and Unicode. any samples for reading a text file which supports all encodings.
Posted
Updated 8-Jan-12 21:43pm
v2

1 solution

Read the file in binary mode into a buffer. Then use ::MultiCharToWideChar to convert the text into the desired format (use ::WideCharToMultiChar to convert UTF-16 to ANSI).

If the encoding of the file is unknown, it can be detected by checking for BOM (Byte Order Mark, present with UCS-2 / UTF-16 and UCS-4 / UTF-32 files, optional with UTF-8 files), and UTF-8 validity checks. If no check matches, it is usually an ANSI encoded file. There are some libraries for codepage detection of ANSI files. However, these detections are guessers that usually return multiple possible codepages.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900