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






3.83/5 (10 votes)
Jan 18, 2006
2 min read

146128

1785
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.