6

I have a stream reader line by line (sr.ReadLine()). My code counts the line-end with both line endings \r\n and/or \n.

        StreamReader sr = new System.IO.StreamReader(sPath, enc);

        while (!sr.EndOfStream)
        {
            // reading 1 line of datafile
            string sLine = sr.ReadLine();
            ...

How to tell to code (instead of universal sr.ReadLine()) that I want to count new line only a full \r\n and not the \n?

procma
  • 1,174
  • 3
  • 14
  • 24
  • So, do you want the number of occurences of `\r\n` and number of occurences of solo `\n` in the stream? – Axarydax Sep 15 '14 at 07:52
  • Exactly I want to read each line, but the line means till ending by the full `\r\n` and not only `\n`. Other words I can say, that one row can contain any `blah blah \n blah \r\n` – procma Sep 15 '14 at 07:58
  • It is important to know how big is your file to choose the correct way to handle the input- – Steve Sep 15 '14 at 08:18

3 Answers3

8

It is not possible to do this using StreamReader.ReadLine. As per msdn:

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 null if the end of the input stream is reached.

So yoг have to read this stream byte-by-byte and return line only if you've captured \r\n

EDIT

Here is some code sample

private static IEnumerable<string> ReadLines(StreamReader stream)
{
    StringBuilder sb = new StringBuilder();

    int symbol = stream.Peek();
    while (symbol != -1)
    {
        symbol = stream.Read();
        if (symbol == 13 && stream.Peek() == 10)
        {
            stream.Read();

            string line = sb.ToString();
            sb.Clear();

            yield return line;
        }
        else
            sb.Append((char)symbol);
    }

    yield return sb.ToString();
}

You can use it like

foreach (string line in ReadLines(stream))
{
   //do something
}
Andrey Korneyev
  • 26,353
  • 15
  • 70
  • 71
4

you cannot do it with ReadLine, but you can do instead:

stream.ReadToEnd().Split(new[] {"\r\n"}, StringSplitOptions.None)
Enrico Sada
  • 909
  • 4
  • 9
  • 2
    What if stream data is really large? ReadToEnd doesn't seems to be a reasonable solution. – Andrey Korneyev Sep 15 '14 at 08:26
  • it always depends on workload, if you need super optimized you can use [StreamReader.Readline source of mono implementation](https://github.com/mono/mono/blob/master/mcs/class/corlib/System.IO/StreamReader.cs) – Enrico Sada Sep 15 '14 at 08:33
-1

For simplification, let's work over a byte array:

    static int NumberOfNewLines(byte[] data)
    {
        int count = 0;
        for (int i = 0; i < data.Length - 1; i++)
        {
            if (data[i] == '\r' && data[i + 1] == '\n')
                count++;
        }
        return count;
    }

If you care about efficiency, optimize away, but this should work.

You can get the bytes of a file by using System.IO.File.ReadBytes(string filename).

Axarydax
  • 16,353
  • 21
  • 92
  • 151