2011-03-01 31 views
1

我已经创建了一个使用FileStream的StreamReader。在StreamReader方法的末尾,使用Peek()方法时,我看到数字值为65535.转换为char时表示句点'。'在VS中使用'watch',我可以看到EndOfStream已经到达。 65535('。')的值是什么意思?它的ASCII码对应的时间是('。')吗?c# - 流读取器结束显示值65536('。')

我以为我听说'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())); 
      } 
+0

它看起来像某种溢出,可以张贴你如何阅读流? – 2011-03-01 16:10:37

回答

7

这意味着你已经铸造 StreamReader.Read()StreamReader.Peek()的结果char检查,看看它是否-1(这意味着它的流的末尾)。首先检查返回值Peek(),如果它是-1则停止。

请注意,流的“逻辑”端可能与流的实际结尾不同。你可能会认为当你到达一个空字符时流会结束,但没有人说它必须达到这个目的,没有人说它不能有更多的数据。所以要小心你正在使用哪一个。

哦,如果你想知道为什么它是65,535 - 那是2^16 - 1这是十六进制的0xFFFF。如果您将-1(即0xFFFFFFFF)投射到char,就会得到这个结果。

+0

StreamReader.EndOfStream属性检查流的“逻辑”结尾还是流的实际结尾? – contactmatt 2011-03-01 16:19:24

+0

“逻辑”端只是你认为是数据结束的地方,但是当确定数据流结束时,计算机不会自己查看数据本身。如果它只是用完更多的数据就结束了;它只检查流的物理结束。 – Mehrdad 2011-03-01 16:20:28

+0

它只有2^16 - 1.我们讲的是16位(和0xFFFF) – xanatos 2011-03-01 16:34:43