473,651 Members | 2,987 Online
Bytes | Software Development & Data Engineering Community
+ 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:\\Docume nts 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::WriteLin e(__box((Char)s r->Read()), " ");
}
}

Oct 15 '05 #1
7 4793
"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:\\Docume nts 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::WriteLin e(__box((Char)s r->Read()), " ");
}
}

Oct 15 '05 #2
Kueishiong Tu <Ku**********@d iscussions.micr osoft.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:\\Docume nts 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::WriteLin e(__box((Char)s r->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.co m>
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**********@d iscussions.micr osoft.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:\\Docume nts 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::WriteLin e(__box((Char)s r->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.co m>
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**********@d iscussions.micr osoft.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.GetEnc oding(950).

--
Jon Skeet - <sk***@pobox.co m>
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.GetEnc oding("big5") and it works.

Kueishiong Tu
"Jon Skeet [C# MVP]" wrote:
Kueishiong Tu <Ku**********@d iscussions.micr osoft.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.GetEnc oding(950).

--
Jon Skeet - <sk***@pobox.co m>
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
28165
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
5502
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; WIN32_FIND_DATA fd;
1
1361
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; this.oleDbCommand1.Parameters.Add("@Type", System.Data.OleDb.OleDbType.WChar, 50, e.Node.Parent.Text); this.oleDbCommand1.Parameters.Add("@Module", System.Data.OleDb.OleDbType.WChar, 50, e.Node.Text);
1
2542
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 ideas how to get this working? void Configure(int port,String* filename,int maxclients) { //const WCHAR* fn = const_cast<__wchar_t*>(PtrToStringChars(filename));
7
366
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 Settings\\kst\\BE.dat"; try { FileStream* fs = new FileStream(path, FileMode::Open); StreamReader* sr = new StreamReader(fs);
2
1618
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 is defined in file ...\platformsdk\include\winnt.h as: typedef __nullterminated CONST WCHAR *LPCWSTR, *PCWSTR; and WCHAR is in turn defined as:
1
6755
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 conversion takes place. For example, does it allocate some new buffer? While debugging an application, I looked into the code of the conversion, but could not understand it. Could you please post the code of conversion and explain it to me? Thanks.
18
19717
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 showing some garbage values like square symbol.how to resolve this.
1
9309
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
8361
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8278
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8701
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6158
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5615
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4144
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4290
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2701
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 we have to send another system
1
1912
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.