views:

597

answers:

3

Hello,

I have a StreamReader that once in a while check if it has more to read from a simple text file. It uses peek property. The problem is that when I am using peek the position is changed, althougth not suppose to.

FileStream m_fsReader = new FileStream(
                        m_strDataFileName,
                        FileMode.OpenOrCreate,
                        FileAccess.Read,
                        FileShare.ReadWrite                        );

StreamReader m_SR = new StreamReader(m_fsReader);

Console.WriteLine("IfCanRead SR Position " + m_fsReader.Position +
     " and Length " + m_fsReader.Length);

if (m_SR.Peek() == -1) {
       Console.WriteLine("IfCanRead false 2 SR Position " + 
             m_fsReader.Position  + " and Length " + m_fsReader.Length);

       return false;
}
else {
       Console.WriteLine("IfCanRead true 2 SR Position " + 
           m_fsReader.Position + " and Length " + m_fsReader.Length);

       return true;
}
+3  A: 

Tested this out myself. Position of the underlying FileStream has changed, but the key point is, that doesn't mean that the StreamReader has actually CONSUMED any bytes. So there is no problem.

Wim Hollebrandse
The ConsoleWriteLine show this
Roman Dorevich
Why on earth you would downvote people who are trying to help you, I have no idea.
Wim Hollebrandse
+1 -- this was exactly the point I was trying to make.
tvanfosson
+9  A: 

The documentation indicates that the position of the StreamReader is not changed, but you are checking the underlying stream's current position, not that of the reader itself. I don't see that it guarantees that the position of the underlying stream remains the same. In fact, I suspect that it simply reads it and buffers internally to keep the reader's cursor at the previous position. This would mean that it doesn't guarantee that the underlying stream's position is unchanged.

The current position of the StreamReader object is not changed by Peek. The returned value is -1 if no more characters are currently available.

tvanfosson
No, I am checking the StreamReader handler.
Roman Dorevich
Your code shows that you are printing out the position of the stream, not the stream reader. I understand that you are calling Peek on the reader, but it appears that you think it leaves the stream in the same position but the documentation only says that it leaves the reader's position unchanged. I'll edit so you can remove your erroneous downvote.
tvanfosson
+1 too. Your elaborate answer does not deserve a negative score.
Wim Hollebrandse
+1 - shame you got a down vote, when actually your answer is bang on the money
Rob Levine
I was told in this site by other people to do that.
Roman Dorevich
@Roman -- downvoting answers that are incorrect is perfectly reasonable and I can understand how you might have thought my answer didn't apply. That's why I commented and edited to give you a chance to undo your vote. No hard feelings.
tvanfosson
A: 

Thanks. It solve my problem.

MK
-1: Not an answer
Casebash
if it helped you just vote the question (+ relevant answers) up...
Yonatan Karni