I had a question about the text-file reading, I just begin to learn this programming environment.
Environmenet: Visual Studio.net 2003
Windows Form Applications (.net C++)
I want to read a text file and put the content into a textbox by using the following statment:
But it seems that there happend a encoding problem during the reading action. The file I want to read is a text-file in Chinese. After running the programme, the characters in the textbox are totally unreadable. It is told that the streamreader is reading a text file with UTF-8 encoding. So, how could read a text file with the Eastern Characters? :confused: Thanks very much!
StreamReader* sr = File::OpenText("C://...//xyz.txt");
inp = sr->ReadToEnd();
textBox1->Text = inp;
File::OpenText returns a StreamReader that uses UTF-8 encoding. Therefore, you need to scan the list of StreamReader constructors to find one that will let you specify an encoding.
One that looks promising:
So next we have to figure out how to get an appropriate Encoding object. Scanning the list of members for the Encoding class gives:
[C++] public: StreamReader(String*, Encoding*);
Initializes a new instance of the StreamReader class for the specified stream, with the specified character encoding
which returns an encoding object for a specified code page.
public: static Encoding* GetEncoding(int);
So putting it all together for encoding of simplified chinese we get:
Alternatively, we can use code page 0 which uses the default code page for the current computer.
// 54936 is the code page for simplified Chinese.
StreamReader * srChinese = new StreamReader("C:\\...\\xyz.txt", System::Text::Encoding::GetEncoding(54936));
There seems to be several Chinese code pages. I don't know which one you use but you can find a list here:
StreamReader * srChinese = new StreamReader("C:\\...\\xyz.txt", System::Text::Encoding::GetEncoding(0));
Thank you very much, anonytmouse, the problem is solved with your help. :)