Click here to Skip to main content
13,899,467 members
Click here to Skip to main content
Add your own
alternative version


27 bookmarked
Posted 18 Jan 2006

Reading or writing a line from/to an ANSI/Unicode encoded text file in Unicode/MBCS applications

, 18 Jan 2006
Rate this:
Please Sign up or sign in to vote.
An article on how to read/write a line from/to a text file encoded by ANSI/Unicode type.


Reading or writing a line from or to a text file maybe a very common task in programming. The CTextFileIO class wants to simplify this task, whatever your text file encoding type be, and your program environment could be ANSI or Unicode. The class properly reads/writes a text file in ANSI, UTF-8, UTF-16 Little Endian, and UTF-16 Big Endian encoding. Its aim is to simplify read/write a line from/to a Unicode or ANSI encoded text file in a MBCS program or in a Unicode program. You don't have to bother about the file's encoding type or your program's environment, it does all this work for you. The class can read or write a text file created by Notepad or any Notepad compatible editor like UltraEdit or EditPlus in ANSI, UTF-8, Unicode, or Unicode Big endian encoding.


When we are programming, we always read or write a line from or to a text file. In the past, maybe this was a very simple task because we just used ANSI encoded text files and our program environment also was ANSI or MBCS. But when we are using Unicode text files or in Unicode programs, it becomes more difficult. So I wrote this class to simplify this task.

Using the code

If you want to use the CTextFileIO class in your program, just add TextFileIO.h and TextFileIO.cpp in your project. Reading or writing a line from a text file is very simple. Declare a CTextFileIO object. Then use the ReadLine or the WriteLine function. It will auto detect the file encoding type and your program environment. Just use the proper function for reading or writing a line.

// First, we declare an CTextFileIO object
CTextFileIO configFile;
// Then, we open the file, notice we should
// always open the file in binary mode
// If your program is Unicode, use OpenW, otherwise use OpenA
// If you want write to file, should use "wb" or "ab"

// For simple, you can just declare an object
// and open it in one step like this
// CTextFileIO configFile(_T("config.txt"),_T("rb"))
// In Unicode application, it's will use OpenW
// and in ANSI program, it's use OpenA function
// Now we read a line from config file
// tstring is std::wstring in Unicode application
// and std::string in others
tstring aLine;
// If you want use LPTSTR instead STL string,
// do just like following
// LPTSTR aLine;
// configFile.ReadLine(aLine); 
// Write a line to file is very like read a line,
// just open the file in "write" or "append" mode
// Then use WriteLine as ReadLine
tstring aLine=_T("a test line");
// It will auto append a EOL at the end of line

Points of Interest

It's very annoying reading a Unicode text file in ANSI (MBCS) program or reading an ANSI file in Unicode program, we always have to worry about the program environment and text file encoding type. At first, I wanted to use the CStdioFile and CString objects to do this job, but that did not work properly.


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

Team Leader
China China
The God created the world.
The programer made the world easy.
I am a programer like c# and c++, so I am writting application with it.
Creating beautiful application make life easy.
If you have a project and looking for a man, I'll be say "hi, I am just the man you are looking for."

You may also be interested in...


Comments and Discussions

GeneralGood job and serious problem [modified] Pin
Harly27-Mar-11 21:08
memberHarly27-Mar-11 21:08 
GeneralOoopss... Pin
Johann Gerell3-Apr-07 3:44
memberJohann Gerell3-Apr-07 3:44 
GeneralVery good Pin
whh20052-Jan-07 20:36
memberwhh20052-Jan-07 20:36 
GeneralIntresting, but some problems Pin
Mingliang Zhu21-Jul-06 22:13
memberMingliang Zhu21-Jul-06 22:13 
AnswerRe: Intresting, but some problems Pin
cokkiy22-Aug-06 22:26
membercokkiy22-Aug-06 22:26 
GeneralNeeds some work Pin
JonesWooHoo17-Apr-06 22:21
memberJonesWooHoo17-Apr-06 22:21 
GeneralRe: Needs some work Pin
Harly27-Mar-11 23:23
memberHarly27-Mar-11 23:23 
Generals.clear() compiler error Pin
elha586-Feb-06 6:41
memberelha586-Feb-06 6:41 
AnswerRe: s.clear() compiler error Pin
cokkiy7-Feb-06 1:02
membercokkiy7-Feb-06 1:02 

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

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

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05 | 2.8.190306.1 | Last Updated 18 Jan 2006
Article Copyright 2006 by cokkiy
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid