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.
CTextFileIO configFile;
configFile.OpenW(L"config.txt",L"rb");
tstring aLine;
configFile.ReadLine(aLine);
tstring aLine=_T("a test line");
configFile.WriteLine(aLine);
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.