I am trying to read a text file with C#, that is formatted like this:
this is a line\r\n this is a line\r \r\n this is a line\r \r\n this is a line\r \r\n this is a line\r\n this is a line\r \r\n etc... I am reading each line from the file with
StreamReader.ReadLine() but that does not preserve new line characters. I need to know/detect what kind of new line characters there are because I am counting the amount of bytes on each line. For example:
if the the line ends with character \r, line consists of: ((nr-of-bytes-in-line) + 1 byte) bytes (depending on the encoding type of course), if line ends with \r\n, line consists of: ((nr-of-bytes-in-line) + 2 bytes) bytes.
EDIT:
I have the solution, based on the answer of israel altar. BTW: Jon Skeet suggested it also. I have implemented an overridden version of ReadLine, so that it would include new line characters. This is the code of the overridden function:
public override String ReadLine() { StringBuilder sb = new StringBuilder(); while (true) { int ch = Read(); if (ch == -1) { break; } if (ch == '\r' || ch == '\n') { if (ch == '\r' && Peek() == '\n') { sb.Append('\r'); sb.Append('\n'); Read(); break; } else if(ch == '\r' && Peek() == '\r') { sb.Append('\r'); break; } } sb.Append((char)ch); } if (sb.Length > 0) { return sb.ToString(); } return null; }
ReadLine()yourself in that case then.string line = sr.ReadLine(); int nrOfBytes = Encoding.GetByteCount(line);But need to detect what kind of new line chars there are.. either\ror\r\n. So that I could do:nrOfBytes += Encoding.GetByteCount(UNKNOWN-NEW-LINE-CHAR);Stream, notStreamReader, because you need to deal with bytes. AllTextReaders, includingStreamReader, help you proces lines at the expense of making it impossible for you to access the raw bytes separating them.