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

I am parsing a very large file of records (one in each line, each of which has a different length), and I would like to keep track of the number of bytes that I read in the file so that I can recover from the failure event.

I wrote the following:

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

However, this will not work, since it ReadLine()removes any CR / LF characters in the string. Alternatively, the line can be terminated with the characters CR, LF or CRLF, which means that I cannot just add 1 to bytesRead.

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

+3
source share
3

, StreamReader , .

, , StreamReader. ?

.

, , N , , ? , ?

+2

TextReader , , [ ] .

, , ? , , , .

+1

Think about it, I can use StreamReaderand get the current position of the underlying stream as follows.

using (StreamReader myTextReader = CreateStreamReader())
{
    stringRecord = myTextReader.ReadLine();
    bytesRead += myTextReader.BaseStream.Position;
    ParseRecord(record);
    // ...
}
0
source

Source: https://habr.com/ru/post/1748282/


All Articles