How to read a text file with wchar?

G

Guest

I have a text file with wide characters. I use the following C++ code to read
them in. However the wide characters are 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()), " ");
}
}
 
G

Guest

"are read in properly" should be "are not read in properly" in the following
description.
 
J

Jon Skeet [C# MVP]

Kueishiong Tu said:
I have a text file with wide characters. I use the following C++ code to read
them in. However the wide characters are 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()), " ");
}
}

Well, what encoding is used for the text file? The default for
StreamReader is UTF-8. If that isn't the encoding used for your file,
you'll get the wrong characters. You really need to know what the
encoding is.

See http://www.pobox.com/~skeet/csharp/unicode.html for more
information.
 
G

Guest

Thank you for replying.
It is a text file contains both Ascii (one byte) and Chinese characters (two
bytes) coded in big-5. How do I find the encoding of a text file?
 
J

Jon Skeet [C# MVP]

Kueishiong Tu said:
Thank you for replying.
It is a text file contains both Ascii (one byte) and Chinese characters (two
bytes) coded in big-5. How do I find the encoding of a text file?

You need to know the encoding - a text file is just a bunch of bytes,
the same as any other file. It's the interpretation which matters.

That sounds like it's just a Big-5 file though - as far as I can see,
ASCII characters come out the same in Big-5.

Big-5 is Windows codepage 950, I believe, so use
Encoding.GetEncoding(950).
 
G

Guest

Thank you very much for your help.
I try Encoding.GetEncoding("big5") and it works.

Kueishiong Tu
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top