StreamReader returns another char
I am trying to read the content of a file using a StreamReader that receives a FileStream. The file has several spaces inside (char 32) and StreamReader reads them as 0 (char 48). The screenshot shows a FileStream buffer and a StreamReader buffer. Both are 32, but when I call Read () it returns 48. Am I missing something? By the way, the code works under the .NET Compact Framework.
alt text http://www.freeimagehosting.net/uploads/9f72b61bbe.png
Code that reads 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 work (neither Unicode nor Default)
EDIT: The input looks like this:
000636920000000532000404100100000001041000000001041000000001031000000000000000000000000000000000000000001730173017301730203020302030203021302130213021300027900267841515150000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000280010000000280010000000280010000020
260007464616011007464816011009005321011009005621011010041621011010041821011013574026011013574226011014564729011014564929011018343318021018343618021020035418021020035618021022583818021022584018021005474302031005474502031010311305031010311505031011265308031011265508031011265508031011274108031021524009
0310215242090310060151130310063110130310160022210310160024210310022837280310022839280310
00206377740002484841000029844400181529330003034081000000000000000000
The problem arises with spaces that start on the third line and go to the fourth.
a source to share
Okay, I just ran a little test. Moving the BaseStream doesn't work for the TextReader, so you're just reading from a different position than you think (and checking the Watch window).
To fix this you will need to create a new StreamReader for each property and be careful not to close it (don't use a use block).
But I would go to read everything at once (it's all text, right?) And work with strings.
a source to share
I suspect this is your problem Encoding.ASCII
. Are you sure your file is encoded this way? I would put your file actually coded with Encoding.Unicode
, so you end up with zeros.
In this case, you are saying that your encoding is UTF-8, so set the encoding Encoding.UTF8
and see what happens.
a source to share
I'm not sure if this is the main problem, but your read loop looks to be wrong.
Try changing it to:
while (reader.Peek() >= 0 && count < offset.Size)
{
char c = (char)reader.Read();
if (c != ' ' && c != '\r' && c != '\n')
{
builder.Append(c);
count++;
}
//else
//{
// reader.BaseStream.Position++;
//}
}
And in the Debugger Clock window, I would use reader.Peek()
, notreader.Read()
Q: Is it possible that your input has a "0"?
a source to share