c# - 流读取器末尾显示值 65536 ('.')

发布于 2024-10-20 01:43:48 字数 1224 浏览 4 评论 0原文

我创建了一个使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暖伴 2024-10-27 01:43:48

这意味着您已将 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 to char before checking to see if it's -1 (meaning it's the end of the stream). Check the return value of Peek() 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 is 0xFFFFFFFF) to a char.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文