locked
converting ASCII file to UTF-8 RRS feed

  • Question

  • Hi,

    I experience a problem related to converting ASCII text file to UTF-8. The following code:

    string p_FileIn = @"c:\z.txt";
    string p_FileOut = @"c:\z_UTF8.txt";
    
    StreamReader sr = new StreamReader(p_FileIn);
    StreamWriter sw = new StreamWriter(p_FileOut, false, Encoding.UTF8);
    
    sw.WriteLine(sr.ReadToEnd());
    
     
    gets executed but produces incorrect results (different from the results obtained from Notepad SaveAs UTF-8).

    Can you please let me know what is wrong with the code fragment above.
    Wednesday, January 6, 2010 3:07 PM

Answers

  • Hi,

    When we use WriteLine method , a carriage return is inserted:
    sw.WriteLine(sr.ReadToEnd());

    Please try sw.Write(sr.ReadToEnd());

    Please also close sr and sw.

    Harry
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Proposed as answer by Harry Zhu Monday, January 11, 2010 7:06 AM
    • Marked as answer by Harry Zhu Wednesday, January 13, 2010 1:30 AM
    Friday, January 8, 2010 3:28 AM

All replies

  • What's different?
    Wednesday, January 6, 2010 3:55 PM
  • Hi,

    When we use WriteLine method , a carriage return is inserted:
    sw.WriteLine(sr.ReadToEnd());

    Please try sw.Write(sr.ReadToEnd());

    Please also close sr and sw.

    Harry
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Proposed as answer by Harry Zhu Monday, January 11, 2010 7:06 AM
    • Marked as answer by Harry Zhu Wednesday, January 13, 2010 1:30 AM
    Friday, January 8, 2010 3:28 AM