473,396 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 4758
"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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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,...

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.