.NET Framework Developer Center > .NET Development Forums > .NET Base Class Library > StreamReader.ReadLine() breaks at single carriage return
Ask a questionAsk a question
 

AnswerStreamReader.ReadLine() breaks at single carriage return

  • Wednesday, July 12, 2006 4:21 PMTom Regan Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    The StreamReader.ReadLine() documentation states that "A line is defined as a sequence of characters followed by a line feed ("\n") or a carriage return immediately followed by a line feed ("\r\n").".

    That means a line should not terminate at a carriage return alone "\r".  But it does.  Complete console application follows.  It is not possible to read a line from a file that contains a carriage return "\r".  It breaks on every carriage return.

    using System;

    using System.Collections.Generic;

    using System.Text;

    using System.IO;

    namespace ReadLineBug

    {

    class Program

    {

    static void Main(string[] args)

    {

    const string fileName = @"C:\Test.txt";

    //create the file

    using (FileStream stream = File.Open(fileName, FileMode.Create))

    {

    using (StreamWriter writer = new StreamWriter(stream))

    {

    writer.Write("a");

    writer.Write(Environment.NewLine);

    writer.Write("a\ra");

    }

    }

    //the file has 2 lines. the second line is a carriage return

    //sandwiched between two "a"'s.

    //read the file line by line. the reader sees 3 lines.

    using (FileStream stream = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))

    {

    bool read = true;

    using (StreamReader reader = new StreamReader(stream))

    {

    while (read)

    {

    string input = reader.ReadLine();

    read = (input != null);

    if (input != null)

    Console.WriteLine("read line from file: '" + input + "'");

    }

    }

    }

    Console.ReadLine();

    }

    }

    }

     

Answers

  • Wednesday, July 12, 2006 9:20 PMRick van den Bosch OLD Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    I looked a bit further: your definition of the StreamReader.ReadLine method is the 1.1 definition (look at the footer). The documentation was edited for the 2.0 version. One of the changes was the addition of the carriage return as a line divider... ;)

    This looks to me like an 'error' in the 1.1 documentation.

All Replies

  • Wednesday, July 12, 2006 9:16 PMRick van den Bosch OLD Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi Tom,

    the online MSDN Library part about StreamReader.ReadLine() states that "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"). The string that is returned does not contain the terminating carriage return or line feed. The returned value is a null reference (Nothing in Visual Basic) if the end of the input stream is reached.".

    Maybe you looked up a wrong version of the ReadLine method? Anyway, the functionality you describe is as designed and follows the documentation.
  • Wednesday, July 12, 2006 9:20 PMRick van den Bosch OLD Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    I looked a bit further: your definition of the StreamReader.ReadLine method is the 1.1 definition (look at the footer). The documentation was edited for the 2.0 version. One of the changes was the addition of the carriage return as a line divider... ;)

    This looks to me like an 'error' in the 1.1 documentation.
  • Thursday, July 13, 2006 12:03 AMTom Regan Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Yes, thanks.  I have Visual Studio 2005 with MSDN, I don't know how I missed the documentation update.