473,587 Members | 2,579 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to read a text file with wide characters?

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

Nov 17 '05 #1
4 2143
>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:\\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()), " ");
}
}


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::Unico de);

Also, why are you reading it char by char when you have the full
capabilities of the StreamReader at your disposal?
--
Tomas Restrepo
to****@mvps.org
http://www.winterdom.com/

Nov 17 '05 #2
What Encoding should I use if the text file contains both Ascii (one byte)
and Chinese characters (2 bytes)? I try Unicode, BigEndianUnicod e, ASCII,
UTF8, UTF7 (the only choices I can see in the Encoding class member), none of
them work.

"Tomas Restrepo (MVP)" wrote:
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:\\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()), " ");
}
}


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::Unico de);

Also, why are you reading it char by char when you have the full
capabilities of the StreamReader at your disposal?
--
Tomas Restrepo
to****@mvps.org
http://www.winterdom.com/

Nov 17 '05 #3
"Kueishiong Tu" <Ku**********@d iscussions.micr osoft.com> wrote in message
news:11******** *************** ***********@mic rosoft.com...
What Encoding should I use if the text file contains both Ascii (one
byte)
and Chinese characters (2 bytes)? I try Unicode, BigEndianUnicod e, ASCII,
UTF8, UTF7 (the only choices I can see in the Encoding class member), none
of
them work.


You need to use Encoding::GetEn coding() 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::GetEn coding("big5").

-cd
Nov 17 '05 #4
Carl:
You are absolutely right. Thank you very much for your and Tomas's help.

Kueishiong Tu

"Carl Daniel [VC++ MVP]" wrote:
"Kueishiong Tu" <Ku**********@d iscussions.micr osoft.com> wrote in message
news:11******** *************** ***********@mic rosoft.com...
What Encoding should I use if the text file contains both Ascii (one
byte)
and Chinese characters (2 bytes)? I try Unicode, BigEndianUnicod e, ASCII,
UTF8, UTF7 (the only choices I can see in the Encoding class member), none
of
them work.


You need to use Encoding::GetEn coding() 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::GetEn coding("big5").

-cd

Nov 17 '05 #5

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

Similar topics

17
10469
by: Guyon Morée | last post by:
what is the difference? if I open a text file in binary (rb) mode, it doesn't matter... the read() output is the same.
2
11144
by: nospam | last post by:
If I had a text file with the following two columns: key1 value1 key2 value2 key3 value3 How would I read in this file and create an associative array?
7
4791
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
2354
by: Profetas | last post by:
I have the following code that detects a <c> and </c> #include <stdio.h> main(int argc, char *argv) { FILE* fp; char data;
7
2241
by: Naren | last post by:
Hello All, Can any one help me in this file read problem. #include <stdio.h> int main() {
35
11396
by: RyanS09 | last post by:
Hello- I am trying to write a snippet which will open a text file with an integer on each line. I would like to read the last integer in the file. I am currently using: file = fopen("f.txt", "r+"); fseek(file, -2, SEEK_END); fscanf(file, "%d", &c); this works fine if the integer is only a single character. When I get into larger...
8
27488
by: Zephyre | last post by:
I have some UTF-8 text files written in Chinese to be read. Now the only method that I know to read text from it is to use fopen() function. Thus, I must read the contents byte by byte, change the UTF-8 characters to Unicode, store the characters into wchar_t variables. But I think this method is too complex and isn't elegant at all. Are...
29
4843
by: list | last post by:
Hi folks, I am new to Googlegroups. I asked my questions at other forums, since now. I have an important question: I have to check files if they are binary(.bmp, .avi, .jpg) or text(.txt, .cpp, .h, .php, .html). How to check a file an find out if the file is binary or text? Thanks for your help.
6
418
by: rohit | last post by:
Hi All, I am new to C language.I want to read integers from a text file and want to do some operation in the main program.To be more specific I need to multiply each of these integers with another set of integers stored in an array.It would be a great help if you could provide some code for it.I tried the function fscanf but by that I am able...
0
7918
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...
0
7843
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...
0
8340
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...
1
7967
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8220
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
3840
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...
0
3875
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2353
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
0
1185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.