Change NewLine for StreamReader

J

jehugaleahsa

Hello:

I am reading a rather oddly formatted file being sent to us by an
outside source. I am wondering if there is a way to change what
StreamReader sees as a newline. It is searching for a \r\n, but the
file has newlines set as \r. Obviously, my StreamReader treats the
whole file as one line.

Is there a way to change the newline constant?

Thanks,
Travis
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Frankly I know of no solution for this. The worst case escenario is using
Read() and discard the char if it's a \r
You could use a StringBuilder to hold the read values.
 
N

Nicholas Paldino [.NET/C# MVP]

Travis,

I would file this as a bug on the Microsoft connect site if that indeed
is the case, because the documentation states:
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
nullNothingnullptra null reference (Nothing in Visual Basic) if the end of
the input stream is reached.

So it should be reading the lines correctly. It doesn't use the NewLine
constant at all.

In the meantime, you can derive a class from StreamReader, and override
the ReadLine method to delimit lines as per your requirements.
 
J

jehugaleahsa

Thank you all. The problem was an encoding problem. I am not sure what
the details are, but when I encoded my file to Unicode, it stopped
working. I probably screwed up while encoding, since I was using a
custom EncodingStream (that I haven't even tested yet).

Thanks, again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top