65.9K
CodeProject is changing. Read more.
Home

Simple class to read and write from an UTF-8 encoded file

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.23/5 (7 votes)

Jul 9, 2004

viewsIcon

72285

downloadIcon

1895

A class derived from CStdioFile to read and write from an UTF-8 encoded file.

Disclaimer

This will certainly look like a very thin article, but it's all in the source :^)

I have looked around quite a bit, both here at Code Project and elsewhere, since I thought that there must be someone who has posted such a class already. Well, I couldn't find any, so here is my own quick hack to solve the problem.

Background

The CStdioFile_UTF8 class was initially done as a step towards making Dan Goodson's excellent TodoList program support Unicode. I'm posting it here in case someone else finds it useful.

Using the code

Use the class as a plug in replacement for MFC CStdioFile. The class overrides the ReadString and WriteString functions in order to do some conversion. It also provides the functions ReadBOM and WriteBOM to handle an optional bute order mark in the file.

If _UNICODE is defined, the UTF16 strings used internally are converted from/to UTF8 as used in the file. If the symbol is not defined, the class acts exactly like the parent class CStdioFile.

History

  • 9-Jul-2004

    Initial version.