StreamReader returning another char

Posted by Fernando on Stack Overflow See other posts from Stack Overflow or by Fernando
Published on 2010-05-14T14:13:33Z Indexed on 2010/05/14 14:14 UTC
Read the original article Hit count: 199

Filed under:
|
|
|

I'm trying to read the content of a file with a StreamReader, that receives a FileStream. The file has some spaces inside (char 32) and the StreamReader is reading them as 0 (char 48). The screenshot shows the FileStream buffer and the StreamReader buffer. Both have the value 32, but when I call Read(), it returns 48. Am I missing something here? By the way, the code is running under .NET Compact Framework.

alt text

The code that reads the data:

public void Read() {
    using (StreamReader reader = new StreamReader(InputStream, Encoding.ASCII)) {
        foreach (var property in DataObject.EnumerateProperties()) {
            OffsetInfo offset = property.GetTextOffset();
            reader.BaseStream.Position = offset.Start - 1;
            StringBuilder builder = new StringBuilder(offset.Size);
            int count = 0;
            while (reader.Peek() >= 0 && count < offset.Size) {
                char c = (char)reader.Read();
                if ((int)c != 32 && c != '\r' && c != '\n')  {
                    builder.Append(c);
                    count++;
                } else {
                    reader.BaseStream.Position++;
                }
            }
            property.SetValue(DataObject,
                Convert.ChangeType(builder.ToString(), property.PropertyType, CultureInfo.CurrentCulture),
                null
            );
        }
    }
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about streamreader