Thread: File Reading..

  1. #1
    Compiling
    Join Date
    Jun 2003
    Posts
    69

    File Reading..

    Greetings all,

    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:
    Code:
    				 String* inp;
    				 StreamReader* sr = File::OpenText("C://...//xyz.txt");
    				 inp = sr->ReadToEnd();
    				 textBox1->Text = inp;
    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? Thanks very much!

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    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:
    Code:
    [C++] public: StreamReader(String*, Encoding*);
    Initializes a new instance of the StreamReader class for the specified stream, with the specified character encoding
    So next we have to figure out how to get an appropriate Encoding object. Scanning the list of members for the Encoding class gives:
    Code:
     public: static Encoding* GetEncoding(int);
    which returns an encoding object for a specified code page.

    So putting it all together for encoding of simplified chinese we get:
    Code:
    // 54936 is the code page for simplified Chinese.
    StreamReader * srChinese = new StreamReader("C:\\...\\xyz.txt", System::Text::Encoding::GetEncoding(54936));
    Alternatively, we can use code page 0 which uses the default code page for the current computer.
    Code:
    StreamReader * srChinese = new StreamReader("C:\\...\\xyz.txt", System::Text::Encoding::GetEncoding(0));
    There seems to be several Chinese code pages. I don't know which one you use but you can find a list here:
    http://66.102.7.104/search?q=cache:X...se+54936&hl=en

  3. #3
    Compiling
    Join Date
    Jun 2003
    Posts
    69
    Thank you very much, anonytmouse, the problem is solved with your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM