Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

CStdioFile::ReadString not working with correctly with UNICODE fil

641 views
Skip to first unread message

Jim Walsh

unread,
May 9, 2007, 5:30:00 PM5/9/07
to
VS2005/C++ and MFC

I am trying to use members of the MFC CStdioFile class to read the contents
of a UNICODE file (created by NOTEPAD).

I can read if if I use _wfopen_s to open the file and fgetws to read the
file.

However, when I open the file with CStdioFile::Open(), and read with
CStdio::ReadString() and examine the contents of the buffer using the
debugger's memory window, I see that each character read from the file is
stored in 16 bits (as I expect), but there is a 16-bit zero between all of
the characters.

Here is a snippet of my code:

CStdioFile f;
CString fileline = _T("");
f.Open(fn, CFile::modeRead);
while (f.ReadString(fileline) && bNotDone) {
.
.

In the debugger, I can see that fileline is using wchar_t for its buffer.
I have set the project Character Set property to "Use Unicode Character Set".

I've searched through the documentation, but haven't be able to find any
solution to this other than to not use CStdioFile.

Thanks in advance for your help.
Jim


Check Abdoul

unread,
May 9, 2007, 9:49:13 PM5/9/07
to
You cannot directly use CStdioFile with UNICODE files. You need to
derive a subclass from CStdioFile and handle the virtual ReadString() &
WriteString() API's to process unicode files that has BOM in it.

Cheers
Check Abdoul
---------------------


"Jim Walsh" <JimW...@discussions.microsoft.com> wrote in message
news:3056A6C8-007F-4EAB...@microsoft.com...

Tom Serface

unread,
May 9, 2007, 11:04:19 PM5/9/07
to
I think the CStdioFile class will work with Unicode files if you compile
your program as Unicode so it uses the Unicode version of MFC. If you
compile as ANSI it only reads ANSI files.

Tom

"Jim Walsh" <JimW...@discussions.microsoft.com> wrote in message
news:3056A6C8-007F-4EAB...@microsoft.com...

David Webber

unread,
May 10, 2007, 2:35:18 AM5/10/07
to

"Tom Serface" <tom.n...@camaswood.com> wrote in message
news:8404C803-C85A-457A...@microsoft.com...

>I think the CStdioFile class will work with Unicode files if you compile
>your program as Unicode so it uses the Unicode version of MFC. If you
>compile as ANSI it only reads ANSI files.

The documentation isn't really explicit on this but I tried this a while
back, and IIRC, CStdioFile *always* assumes one is reading the old fashioned
1 byte per character text files. If you compile with UNICODE it just
reads them into wide strings; it doesn't read Unicode text files.

But the ReadString() method is virtual as Check Abdoul says, so one can
override it.

Dave
--
David Webber
Author of 'Mozart the Music Processor'
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mzusers/mailinglist.htm

Victor

unread,
May 10, 2007, 6:23:06 AM5/10/07
to
Yes, the problem does really exist!
And when I once had similar problems I just replaced the
CStdioFile::ReadString with CFile::Read method :(.
However, some other workarounds are also possible, like using
CFile::typeBinary for the CStdioFile file opened for reading.
See this thread:
http://groups.google.de/group/microsoft.public.vc.mfc/browse_frm/thread/67740273a8a317c0/a6d4acce4952c946?lnk=st&q=CStdioFile+with+Unicode+&rnum=1#a6d4acce4952c946

Regards,
Victor

"Jim Walsh" <JimW...@discussions.microsoft.com> wrote in message
news:3056A6C8-007F-4EAB...@microsoft.com...

Jim Walsh

unread,
May 10, 2007, 10:27:20 AM5/10/07
to
Thanks to everyone who took the time to answer my posting. You've confirmed
my experience.

Very frustrating to blow away a day trying to get this to work.

My work around using _wfopen_s and fgetws works fine.

Do you think that there is any advantage to me in pursuing an override of
CStdioFile::ReadString()?

Jim

Tom Serface

unread,
May 10, 2007, 3:27:41 PM5/10/07
to
Jim,

Send me an email and I'll send you a class where this is already done. It's
too much to post here.

Tom

"Jim Walsh" <JimW...@discussions.microsoft.com> wrote in message

news:E16F025F-8C25-463A...@microsoft.com...

Joseph M. Newcomer

unread,
May 11, 2007, 12:15:13 AM5/11/07
to
Tom, if you want me to post it on my site, I'll put it up for you (and credit you
directly)
joe

Joseph M. Newcomer [MVP]
email: newc...@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

0 new messages