Click here to Skip to main content
15,885,216 members
Articles / Desktop Programming / MFC
Article

CStdioFile-derived class for multibyte and Unicode reading and writing

Rate me:
Please Sign up or sign in to vote.
4.91/5 (67 votes)
19 Jul 2007CPOL3 min read 1M   14.1K   110   171
A class, derived from CStdioFile, which transparently reads and writes both Unicode and multibyte files. Version 1.5.

Demo app screenshot

Introduction

This is a class derived from CStdioFile which transparently handles the reading and writing of Unicode text files as well as ordinary multibyte text files.

The code compiles as both multibyte and Unicode. In Unicode, multibyte files will be read and their content converted to Unicode using the current code page. In multibyte compilations, Unicode files will be read and converted to multibyte text.

The identification of a Unicode text file depends entirely on the presence of the Unicode byte order mark (0xFEFF). Its absence is not an absolute guarantee that a file is not Unicode, but it's the only method I use here. Feel free to suggest improvements.

By default, the class writes multibyte files, but can optionally write Unicode.

Background

The ability to transparently handle both multibyte and Unicode seems to be such a fundamental requirement, that I was sure that there would already be something similar on offer, and yet nothing turned up. Did I miss something?

I needed it for a translation tool I wrote, and knocked together an implementation that was good enough for my needs. This is little more than a cleaned up version of that, so expect bugs and all manner of deficiencies. I've tested the demo app though with the basic combinations -- Unicode files in a multibyte compilation, Unicode-Unicode, Multibyte-Unicode, and Multibyte-Multibyte, and they all seem to work.

Using the code

The use of the class is pretty simple. It overrides three functions of CStdioFile: Open(), ReadString() and WriteString(). To write a Unicode file, add the flag CStdioFileEx::modeWriteUnicode to the flags when calling the Open() function.

In other respects, usage is identical to CStdioFile.

To find out if a file you have opened is Unicode, you can call IsFileUnicodeText().

To get the number of characters in the file, you can call GetCharCount(). This is unreliable for multibyte/UTF-8, however.

An example of writing in Unicode:

C++
// Test writing
CStdioFileEx fileWriteUnicode;

if (fileWriteUnicode.Open(_T("c:\\testwrite_unicode.txt"), 
    CFile::modeCreate | CFile::modeWrite | CStdioFileEx::modeWriteUnicode))
{
    fileWriteUnicode.WriteString(_T("Unicode test file\n"));
    fileWriteUnicode.WriteString(_T("Writing data\n"));
    fileWriteUnicode.Close();
}

You can now also specify the code page for multibyte file reading or writing. Simply call SetCodePage() before a read to tell CStdioFileEx which code page the file is coded in, or before a write, to tell it which code page you want it written in. Specifying CP_UTF8 as the code page allows you to read or write UTF-8 files.

The demo app is a dialog which opens a file, tells you whether it's Unicode or not and how many characters it contains, and shows the first fifteen lines from it. In the last couple of iterations I've added the option to convert a Unicode file to multibyte, and a multibyte file to Unicode, and a combo to specify the code page when reading.

As of v1.6, there is no limitation on the length of the line that can be read in any mode (Multibyte/Unicode, Unicode/Multibyte, etc.).

I'd love to hear of people's experiences with it, as well as reports of bugs, problems, improvements, etc.

Oh, and if I've accidentally included something offensive in the demo dialog, let me know. My Arabic and Chinese are not all that good.

History

  • v1.0 - Posted 14 May 2003
  • v1.1 - 23 August 2003. Incorporated fixes from Dennis Jeryd
  • v1.2 - 06 January 2005. Fixed garbage at end of file bug (Howard J Oh)
  • v1.3 - 19 February 2005. Howard J Oh's fix mysteriously failed to make it into the last release. Improved the test program. Fixed miscellaneous bugs
    Very important: In this release, ANSI files written in ANSI are no longer written using WriteString. This means \n will no longer be "interpreted" as \r\n. What you write is what you get
  • v1.4 - 26 February 2005. Fixed submission screw-up
  • v1.5 - 18 November 2005. Code page can be specified for reading and writing (inc. UTF-8). Multibyte buffers properly calculated. Fix from Andy Goodwin
  • v1.6 - 19 July 2007. Major rewrite: Maximum line length restriction removed; Use of strlen/lstrlen eliminated. Conversion functions always used to calculate required buffers; \r or \n characters no longer lost; BOM writing now optional; UTF-8 reading and writing works properly; systematic tests are now included with the demo project

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Spain Spain
I'm originally from Leek, Staffordshire in the UK, but I now work as a C++/MFC developer in Madrid, Spain.

I followed an erratic study/career path from German to a PhD in something resembling political science and linguistics, eventually ending up in IT.

I'm still finding bustling streets, warm nights, beer and vitamin D a pretty heady combination.

Comments and Discussions

 
GeneralA very nice class Pin
copernican3-Feb-06 22:33
copernican3-Feb-06 22:33 
GeneralRe: A very nice class Pin
David Pritchard5-Feb-06 3:09
David Pritchard5-Feb-06 3:09 
QuestionHelp, Release compiling error? Pin
peter_ywf29-Nov-05 2:07
peter_ywf29-Nov-05 2:07 
AnswerRe: Help, Release compiling error? Pin
David Pritchard29-Nov-05 8:56
David Pritchard29-Nov-05 8:56 
GeneralRe: Help, Release compiling error? Pin
peter_ywf1-Dec-05 4:25
peter_ywf1-Dec-05 4:25 
GeneralRe: Help, Release compiling error? Pin
David Pritchard1-Dec-05 9:57
David Pritchard1-Dec-05 9:57 
GeneralNon-English ANSII characters are not REALLY converted into Unicode by Windows XP Pin
chesscafe200328-Oct-05 21:55
chesscafe200328-Oct-05 21:55 
GeneralRe: Non-English ANSII characters are not REALLY converted into Unicode by Windows XP Pin
David Pritchard17-Nov-05 13:14
David Pritchard17-Nov-05 13:14 
Hi, sorry I've taken a while to reply. I've been meaning to update the project for ages and only got around to it today, and realised I hadn't replied to you.

I can't just see any answer to your puzzle (at 1:12 in the morning anyway). Could you send me the ANSI file which isn't read properly, so I can try it here? Send it to davidpritchardNO_SPAM@ctv.es (remove the anti-spam bit first!). Thanks!

I'm going to update the project now with an option to specify the code page when reading a non-Unicode file. At first glance it doesn't look like this is your problem, but it may help.
GeneralRe: Non-English ANSII characters are not REALLY converted into Unicode by Windows XP Pin
chesscafe200325-Nov-05 5:33
chesscafe200325-Nov-05 5:33 
GeneralRe: Non-English ANSII characters are not REALLY converted into Unicode by Windows XP Pin
David Pritchard25-Nov-05 8:10
David Pritchard25-Nov-05 8:10 
GeneralRe: Non-English ANSII characters are not REALLY converted into Unicode by Windows XP Pin
chesscafe20032-Dec-05 12:53
chesscafe20032-Dec-05 12:53 
GeneralGreat Class Pin
chesscafe20038-Oct-05 21:57
chesscafe20038-Oct-05 21:57 
GeneralRe: Great Class Pin
David Pritchard9-Oct-05 2:29
David Pritchard9-Oct-05 2:29 
QuestionANSI Pin
Member 21955837-Sep-05 20:19
Member 21955837-Sep-05 20:19 
AnswerRe: ANSI Pin
David Pritchard18-Sep-05 3:31
David Pritchard18-Sep-05 3:31 
GeneralRe: ANSI Pin
Member 219558320-Sep-05 5:47
Member 219558320-Sep-05 5:47 
GeneralRe: ANSI Pin
David Pritchard20-Sep-05 8:16
David Pritchard20-Sep-05 8:16 
GeneralRe: ANSI Pin
David Pritchard25-Sep-05 0:32
David Pritchard25-Sep-05 0:32 
GeneralRe: ANSI Pin
Mihai Nita25-Nov-05 10:00
Mihai Nita25-Nov-05 10:00 
GeneralBug in source code, Useful class Pin
Andy Goodwin25-Aug-05 2:19
Andy Goodwin25-Aug-05 2:19 
GeneralRe: Bug in source code, Useful class Pin
David Pritchard25-Aug-05 7:56
David Pritchard25-Aug-05 7:56 
Generalcompile DLL problem. Pin
amanofsky13-Jun-05 2:13
amanofsky13-Jun-05 2:13 
GeneralRe: compile DLL problem. Pin
David Pritchard13-Jun-05 8:56
David Pritchard13-Jun-05 8:56 
GeneralUnicode Text File Pin
c_srishti5-Jun-05 7:48
c_srishti5-Jun-05 7:48 
GeneralRe: Unicode Text File Pin
David Pritchard5-Jun-05 8:06
David Pritchard5-Jun-05 8:06 

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.