Click here to Skip to main content
Click here to Skip to main content

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

, 8 Jul 2004
Rate this:
Please Sign up or sign in to vote.
A class derived from CStdioFile to read and write from an UTF-8 encoded file.


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

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.


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.


  • 9-Jul-2004

    Initial version.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Sven Axelsson
Web Developer
Sweden Sweden
No Biography provided

Comments and Discussions

GeneralCompiler Error Pinmemberd00_ape30-Aug-04 0:10 
GeneralRe: Compiler Error PinsussUwe Sedlack11-Mar-05 7:12 
GeneralCStdioFileEx PinmemberDavid Pritchard14-Jul-04 12:51 
Generalhalf-baked conversion Pinmemberumeca7413-Jul-04 20:51 
your ReadString method converts from utf8 -> windows text if building with unicode but for ansi it just leaves the multibyte text which isn't much use if you want e.g. to paste the text in an edit control.
your use of MultiByteToWideChar needs to be revised too
you read the file in chunks of nMax characters, which may happen to break a multibyte sequence. As your code stands now you will be losing all such characters
i'm glad to see utf8 interest since the world is much bigger than english-speaking countries!
GeneralRe: half-baked conversion PinmemberSven Axelsson16-Jul-04 1:42 
GeneralRe: half-baked conversion Pinmemberumeca7419-Jul-04 4:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150302.1 | Last Updated 9 Jul 2004
Article Copyright 2004 by Sven Axelsson
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid