tags:

views:

114

answers:

1

Is there any function in c# equivalent to C's stdio function ungetc() ?

+4  A: 

Not that I know of; for some scenarios, you could use something like a StreamReader and use Peek?

        using(StreamReader reader = new StreamReader(stream))
        {
            /// ... lots of reading
            int i = reader.Peek();
            /// ... lots of reading
        }

However, I don't think you can push arbitrary data back into the stream unless you are using something like MemoryStream and can thus monkey with the data.

Even if you have a Stream that is both readable and writeable (rare), then there is still only one cursor, so you would need to be careful to reset the position after writing (so it would need to be seekable too; again, not common); however, this is not robust - some streams (the network streams in TCP, for example) would treat the write as a "send this to the other machine", and it would never be returned by a read. And it isn't seekable ;-p

Marc Gravell