开发者

c# - end of stream reader showing value 65536 ('.')

开发者 https://www.devze.com 2023-02-13 23:44 出处:网络
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 \'.

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 consumin开发者_运维百科g 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()));
            }


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消