Target file:
Hello
World
Code:
if (file != null)
{
//Read file one character at a time
StreamReader reader;
reader = new StreamReader(file);
do
{
int s = reader.Read();
char sC = (char)s;
if (sC.Equals(Environment.NewLine))
{
Console.WriteLine("+1");
}
Console.WriteLine((char)s);
} while (!reader.EndOfStream);
reader.Close();
reader.Dispose();
}
Output:
H
e
l
l
o
W
o
r
l
d
So (sC.Equals(Environment.NewLine)) obviously doesn’t detect a platform independent newline when reading one char at a time.. how do I do this?
try this instead
If you are sure there will always be a
\rafter the\nthen you can swallow it with anotherreader.Read();call. Example: