how can i read Chinese using C#

  • Thread starter Thread starter Cheryl
  • Start date Start date
C

Cheryl

Hi!
I am implementing a system that can read Chinese characters from a text file
and do some string manipulations. Can anyone suggest a method on how I can
do file I/O with Chinese?
Thanks.
 
The same as usual - except you have to be careful which character
encoding you are using. Take a look at the available constructors for
FileStream etc - you can specify the encoding there (you'll probably
want to use Unicode)
 
I have tried to change the encoding, but then the results seems strange:

Code:
try
{

using (StreamReader sr = new
StreamReader(path,System.Text.Encoding.UTF8))
{

//This is an arbitrary size for this example.
string c = null;

while (sr.Peek() >= 0)
{
c = null;
c = sr.ReadLine();
Console.WriteLine(c);

}
}
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}




Result:
SPOT !UA!U!nXqA!nXq!?{!?!i{!Es!_A!?!H!i{!H!
HERE !?!?!??a!??a!??a!a!??a!??a!
 
Cheryl said:
I have tried to change the encoding, but then the results seems strange:

Sure, if you try using UTF-8 and it's not actually in UTF-8, it will
look odd. You need to *know* the appropriate encoding - the encoding
the file was saved in to start with. What created the file in the first
place?
 
Back
Top