473,468 Members | 1,471 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to read a text file with wchar?

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()), " ");
}
}

Oct 15 '05 #1
7 4761
"are read in properly" should be "are not read in properly" in the following
description.

"Kueishiong Tu" wrote:
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()), " ");
}
}

Oct 15 '05 #2
Kueishiong Tu <Ku**********@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 15 '05 #3
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?

"Jon Skeet [C# MVP]" wrote:
Kueishiong Tu <Ku**********@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Oct 15 '05 #4
Kueishiong Tu <Ku**********@discussions.microsoft.com> wrote:
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).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 15 '05 #5
Kueishiong,

Only in addition to Jon's answers

Unicode
http://www.geocities.com/Athens/Acad.../fontset.htm#b

OS systems
http://www.microsoft.com/globaldev/r...ocversion.mspx

I hope this helps a little bit?

Cor
Oct 15 '05 #6
Thank you very much for your help.

Kueishiong Tu

"Cor Ligthert [MVP]" wrote:
Kueishiong,

Only in addition to Jon's answers

Unicode
http://www.geocities.com/Athens/Acad.../fontset.htm#b

OS systems
http://www.microsoft.com/globaldev/r...ocversion.mspx

I hope this helps a little bit?

Cor

Oct 16 '05 #7
Thank you very much for your help.
I try Encoding.GetEncoding("big5") and it works.

Kueishiong Tu
"Jon Skeet [C# MVP]" wrote:
Kueishiong Tu <Ku**********@discussions.microsoft.com> wrote:
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).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Oct 16 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Flzw | last post by:
How to convert a std::string to a WCHAR* ? is there any methods or something ? I can't find. Thanks
3
by: Lars Nielsen | last post by:
Hey there I have a win32 application written i c++. I have a std::vector of std::string's i will fill with filenames. typedef vector<std::string> strvector; strvector vFiles; ...
1
by: ST Choong | last post by:
this.oleDbCommand1.CommandText = "SELECT Description, , Bit FROM IO WHERE (Type = \'@Type\' AND Module = \'@Module\')"; this.oleDbCommand1.Connection = this.oleDbConnection1; ...
1
by: Kieran Benton | last post by:
Hi, Sorry to post this, I feel like a right fool but Im under serious time pressure! Afraid I'm a newbie to managed C++ (Ive had to resort to it as Im wrapping some COM objects for C# use). Any...
7
by: Kueishiong Tu | last post by:
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...
2
by: acc13 | last post by:
I have written a .dll that exports a class MyClass, which has a member function MyFunction(LPCWSTR szMyString). If I build (I'm using VC7) with the /showIncludes option, I can see that LPCWSTR...
1
by: AvinashS | last post by:
I have a WCHAR array (say WCHAR *wszName). I want to convert it to wstring. So, I do the following:- wstring wstrName(wszName); It works. But I want to know what internally happens when the...
18
by: Manjunath.M | last post by:
Hi, I wrote a simple program. WCHAR NameBuffer; char * str1 = "c:\\Program Files\\test.txt" ; swprintf(NameBuffer,L"%s",str1); I tried to debug this program. value at "Namebuffer" is...
1
by: sandeepkavade | last post by:
hi all i want to convert wstrint to WCHAR* how to do that? i am getting following error: error C2664: 'UnicodeToUtf8' : cannot convert parameter 1 from 'std::wstring *__w64 ' to 'const WCHAR *'
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.