Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / October 2005

Tip: Looking for answers? Try searching our database.

How to read a text file with wide characters?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Kueishiong Tu - 15 Oct 2005 09:41 GMT
I have a text file with wide characters. I use the following C++ code to read
them in. However the wide characters are not read in properly. What is wrong?

   String* path = "C:\\Documents and Settings\\kst\\BE.dat";
           
   try
   {
           FileStream* fs = new FileStream(path, FileMode::Open);
          StreamReader* sr = new StreamReader(fs);

          int count = 0;
          while (sr->Peek() >= 0)
          {
    count++;
               Debug::Write(__box(count));
    Debug::WriteLine(__box((Char)sr->Read()), " ");
          }
     }
           
Tomas Restrepo (MVP) - 15 Oct 2005 12:59 GMT
>I have a text file with wide characters. I use the following C++ code to
>read
[quoted text clipped - 16 lines]
>           }
>      }

StreamReader defaults to UTF-8 encoding. If you want to use UTF-16 or
Unicode (or any other encoding, for that matter) you need to especify it in
the constructor. Example:

using System::Text;
StreamReader* sr = new StreamReader(fs, Encoding::Unicode);

Also, why are you reading it char by char when you have the full
capabilities of the StreamReader at your disposal?

Signature

Tomas Restrepo
tomasr@mvps.org
http://www.winterdom.com/

Kueishiong Tu - 15 Oct 2005 16:56 GMT
What Encoding should I use if the text file contains both Ascii (one  byte)
and Chinese characters (2 bytes)? I try Unicode, BigEndianUnicode, ASCII,
UTF8, UTF7 (the only choices I can see in the Encoding class member), none of
them work.

> >I have a text file with wide characters. I use the following C++ code to
> >read
[quoted text clipped - 26 lines]
> Also, why are you reading it char by char when you have the full
> capabilities of the StreamReader at your disposal?
Carl Daniel [VC++ MVP] - 15 Oct 2005 18:15 GMT
> What Encoding should I use if the text file contains both Ascii (one
> byte)
> and Chinese characters (2 bytes)? I try Unicode, BigEndianUnicode, ASCII,
> UTF8, UTF7 (the only choices I can see in the Encoding class member), none
> of
> them work.

You need to use Encoding::GetEncoding() to find the appropriate encoding.
The ASCII, UTF8, ... members of the Encoding class are just shortcuts for
the most commonly used encodings.

In your case, you probably want something like
Encoding::GetEncoding("big5").

-cd
Kueishiong Tu - 16 Oct 2005 15:06 GMT
Carl:
You are absolutely right. Thank you very much for your and Tomas's help.

Kueishiong Tu

> > What Encoding should I use if the text file contains both Ascii (one
> > byte)
[quoted text clipped - 11 lines]
>
> -cd

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.