gpt4 book ai didi

c# - 一次读取一个字符的文本文件时检测换行

转载 作者:太空宇宙 更新时间:2023-11-03 18:13:16 26 4
gpt4 key购买 nike

目标文件:

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com