c# - 流读取器末尾显示值 65536 ('.')
我创建了一个使用 FileStream 的 StreamReader。在 StreamReader 方法的末尾,使用 Peek() 方法时,我看到数值 65535。转换为 char 时表示句点 '.'使用 VS 中的“手表”,我可以看到已到达 EndOfStream。 65535('.')值是什么意思?句点 ('.') 是其 ASCII 对应项吗?
我以为我听说“0”代表文件/流的结尾。
注意:如果流正在消耗文件,我不确定 EOF 和 EOS(流结束)之间是否存在差异。
//Contains some business logic, main focus is on the while loop expression
try
{
//The peek method is used to avoid moving the Stream's position.
//If we don't encounter a number character representing the RDW, keep reading until we find one.
while (!Char.IsDigit((char)this.StreamReader.Peek()))
{
if (!this.StreamReader.EndOfStream)
this.StreamReader.Read();
else
return false;
}
//Loop completed and found the next record without encountering the end of the stream
return true;
}
catch (IOException IOex)
{
throw new Exception(String.Format("An IO Exception occured when attempting to set the start position of the record.\n\n{0}", IOex.ToString()));
}
I have created a StreamReader that uses a FileStream. At the end of the StreamReader method, when using the Peek() method, I see the numeric value 65535. When converted to a char represents a period '.' Using a 'watch' in VS, I can see that the EndOfStream has been reached. What does the 65535 ('.') value mean? Is the period ('.') its ASCII counterpart?
I had thought I heard that a '0' represents the end of a file/stream.
Note: I'm not sure if there's a difference between the EOF and EOS (End of stream) if the stream is consuming a file.
//Contains some business logic, main focus is on the while loop expression
try
{
//The peek method is used to avoid moving the Stream's position.
//If we don't encounter a number character representing the RDW, keep reading until we find one.
while (!Char.IsDigit((char)this.StreamReader.Peek()))
{
if (!this.StreamReader.EndOfStream)
this.StreamReader.Read();
else
return false;
}
//Loop completed and found the next record without encountering the end of the stream
return true;
}
catch (IOException IOex)
{
throw new Exception(String.Format("An IO Exception occured when attempting to set the start position of the record.\n\n{0}", IOex.ToString()));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这意味着您已将
StreamReader.Read()
的StreamReader.Peek()
的结果转换为char
之前检查是否为-1
(意味着它是流的结尾)。首先检查Peek()
的返回值,如果是-1
则停止。请注意,流的“逻辑”结束可能与流的实际结束不同。当到达空字符时,您可能会认为流即将结束,但没有人说它必须达到这一点,也没有人说它不能有更多数据。因此,请注意与您合作的人。
哦,如果您想知道为什么是 65,535,那就是
2^16 - 1
,即十六进制的 0xFFFF。这就是将-1
(即0xFFFFFFFF
)转换为char
时得到的结果。It means you've casted
StreamReader.Read()
'sStreamReader.Peek()
's result tochar
before checking to see if it's-1
(meaning it's the end of the stream). Check the return value ofPeek()
first, and stop if it's-1
.Note that the "logical" end of a stream might be different from the actual end of the stream. You might consider the stream to be ending when you reach a null character, but no one says it ever has to reach that, and nothing says it can't have more data. So be careful about which one you're working with.
Oh, and in case you're wondering why it's 65,535 -- that's
2^16 - 1
which is 0xFFFF in hexadecimal. It's what you get if you cast-1
(which is0xFFFFFFFF
) to achar
.