Search code examples
c#.netreadlinetextreader

How do I count the number of bytes read by TextReader.ReadLine()?


I am parsing a very large file of records (one per line, each of varying length), and I'd like to keep track of the number of bytes I've read in the file so that I may recover in the event of a failure.

I wrote the following:

using (TextReader myTextReader = CreateTextReader())
{
    string record = myTextReader.ReadLine();
    bytesRead += record.Length;
    ParseRecord(record);
}

However this doesn't work since ReadLine() strips any CR/LF characters in the line. Furthermore, a line may be terminated by either CR, LF, or CRLF characters, which means I can't just add 1 to bytesRead.

Is there an easy way to get the actual line length, or do I write my own ReadLine() method in terms of the granular Read() operations?


Solution

  • Getting the current position of the underlying stream won't help, since the StreamReader will buffer data read from the stream.

    Essentially you can't do this without writing your own StreamReader. But do you really need to?

    I would simply count the number of lines read.

    Of course, this means that to position to a specific line you will need to read N lines rather than simply seeking to an offset, but what's wrong with that? Have you determined that performance will be unacceptable?