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

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.

Introduction

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.

Background

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
configFile.OpenW(L"config.txt",L"rb");
// 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;
configFile.ReadLine(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");
configFile.WriteLine(aLine);
// 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.

License

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

Share

About the Author

cokkiy
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."

Comments and Discussions

 
GeneralGood job and serious problem [modified] PinmemberHarly27-Mar-11 22:08 
GeneralOoopss... PinmemberJohann Gerell3-Apr-07 4:44 
GeneralVery good Pinmemberwhh20052-Jan-07 21:36 
GeneralIntresting, but some problems PinmemberPerry Zhu21-Jul-06 23:13 
AnswerRe: Intresting, but some problems Pinmembercokkiy22-Aug-06 23:26 
GeneralNeeds some work PinmemberJonesWooHoo17-Apr-06 23:21 
GeneralRe: Needs some work PinmemberHarly28-Mar-11 0:23 
Generals.clear() compiler error Pinmemberelha586-Feb-06 7:41 
AnswerRe: s.clear() compiler error Pinmembercokkiy7-Feb-06 2:02 

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.141223.1 | Last Updated 18 Jan 2006
Article Copyright 2006 by cokkiy
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid