目标文件:
Hello
World
码:
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();
}
输出:
H
e
l
l
o
W
o
r
l
d
所以
(sC.Equals(Environment.NewLine))
一次读取一个char时显然不会检测到与平台无关的换行符。我该怎么做?
最佳答案
试试这个
if (sC.Equals('\n'))
如果您确定在
\r
之后总会有一个\n
,则可以通过另一个reader.Read();
调用将其吞下。例:if (sC.Equals('\n')) {
reader.Read()
Console.WriteLine("+1");
}
关于c# - 一次读取一个字符的文本文件时检测换行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10785960/