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

Posted by Steve Guidi on Stack Overflow See other posts from Stack Overflow or by Steve Guidi
Published on 2010-06-03T06:08:54Z Indexed on 2010/06/03 6:14 UTC
Read the original article Hit count: 221

Filed under:
|
|
|

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:

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?

© Stack Overflow or respective owner

Related posts about c#

Related posts about .NET