none
How do I have StreamReader ReadLine() read all chars, including 0x\0D? RRS feed

  • Question

  • I have an old .txt file I am trying to covert.  Many lines (but not all) end with "0x0D 0x0D 0x0A".

    Is there a way to have StreamReader ReadLine() read all of the chars including the "0x0D 0x0D 0x0A"


    bhs67

    Friday, February 27, 2015 10:20 PM

Answers

  • The file is very large.  Is there a way to read "all" chars, perhaps 20 chars at a time.

    bhs67


    You could use the Read method to read a specified maximum of characters from the current stream into a buffer: https://msdn.microsoft.com/en-us/library/9kstw824%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

    using (StreamReader sr = new StreamReader("yourfile.txt", System.Text.Encoding.ASCII))
                {
                    char[] c = null;
    
                    int take = 20;
                    while (sr.Peek() >= 0)
                    {
                        c = new char[take];
                        sr.Read(c, 0, take);
    
                        //read characters:
                        string s = new string(c);
                    }
                }

    But please don't ask several questions in the same thread. Close your threads by marking helpful posts as answer and then start a new thread if you have a new question.


    • Edited by Magnus (MM8)MVP Saturday, February 28, 2015 5:54 PM
    • Marked as answer by bhs67 Saturday, February 28, 2015 8:26 PM
    Saturday, February 28, 2015 5:54 PM

All replies

  • The ReadLine function docs define a 'line':

    "A line is defined as a sequence of characters followed by a line feed ("\n"), a carriage return ("\r"), or a carriage return immediately followed by a line feed ("\r\n")."

    I take it that two ReadLine calls will read that sequence? Its two lines by definition, just discard the empty one if that's what you want in the converted text.

    Friday, February 27, 2015 10:37 PM
  • I could do that.  However, sometimes there is a space between the lines.  I don't want to discard the spaces.  Am I missing something?

    bhs67

    Friday, February 27, 2015 11:07 PM
  • If your old text file isn't to large, I have used as below to read old DOS SPSS program generated script files and keeping \n\r:

    using (StreamReader reader = new StreamReader(@"D:\OLD_DOS_TEXT.txt", System.Text.Encoding.ASCII))
    {
       string txt= reader.ReadToEnd();
    }



    Saturday, February 28, 2015 2:32 AM
  • The file is very large.  Is there a way to read "all" chars, perhaps 20 chars at a time.

    bhs67

    Saturday, February 28, 2015 5:18 PM
  • The file is very large.  Is there a way to read "all" chars, perhaps 20 chars at a time.

    bhs67


    You could use the Read method to read a specified maximum of characters from the current stream into a buffer: https://msdn.microsoft.com/en-us/library/9kstw824%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

    using (StreamReader sr = new StreamReader("yourfile.txt", System.Text.Encoding.ASCII))
                {
                    char[] c = null;
    
                    int take = 20;
                    while (sr.Peek() >= 0)
                    {
                        c = new char[take];
                        sr.Read(c, 0, take);
    
                        //read characters:
                        string s = new string(c);
                    }
                }

    But please don't ask several questions in the same thread. Close your threads by marking helpful posts as answer and then start a new thread if you have a new question.


    • Edited by Magnus (MM8)MVP Saturday, February 28, 2015 5:54 PM
    • Marked as answer by bhs67 Saturday, February 28, 2015 8:26 PM
    Saturday, February 28, 2015 5:54 PM