views:

97

answers:

2

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.UTF8)) {
  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
   );
  }
 }
}

EDIT: Changing the encoding didn't worked (neither Unicode, nor Default)

EDIT: The input looks like this:

000636920000000532000404100100000001041000000001041000000001031000000000000000000000000000000000000000001730173017301730203020302030203021302130213021300027900267841515150000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000280010000000280010000000280010000020
260007464616011007464816011009005321011009005621011010041621011010041821011013574026011013574226011014564729011014564929011018343318021018343618021020035418021020035618021022583818021022584018021005474302031005474502031010311305031010311505031011265308031011265508031011265508031011274108031021524009
0310215242090310060151130310063110130310160022210310160024210310022837280310022839280310                                                                                                                                                                                                                    
                                                                                                                                                                                                                                        00206377740002484841000029844400181529330003034081000000000000000000

The problem happens with the spaces that start in the third line and goes to the fourth.

+5  A: 

I suspect your problem is the Encoding.ASCII. Are you positive your file is encoded this way? I'd wager your file is actually encoded with Encoding.Unicode, which is why you're encountering zeroes.

In this case you say your encoding is UTF-8, so set your encoding to Encoding.UTF8 and see what happens.

Doug
Or use `Encoding.Default` which is the default encoding on your windows system (not the default for `StreamReader` as one could easily think...)
awe
It didn't work. The file is UTF-8. But changing that doesn't solves the problem.
Fernando
+1  A: 

OK, I just ran a little test. Repositioning the BaseStream doesn't work for a TextReader, so you are simply reading from another position than you think you are (and are checking in the Watch window).

To solve it, you will have to create a new StreamReader for each property, and be careful not to close it (don't use a using block).

But I would go for reading it all at once (it is all text, right?) and operate on the string(s).

Henk Holterman
Altough I've not used your solution, your test made me think about calling reader.DiscardBufferedData(), and that did the trick. Thanks!
Fernando