473,403 Members | 2,183 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

StreamReader and Peek()

Dan
Hi, I'm having a problem with StreamReader.Peek(). Let's say I open a file
and read it to end; then I'd want to move its stream pointer back to the
file beginning: I can call BaseStream Seek method or change the value of the
Position property, but this does not seem to affect the Peek() method, which
keeps returning -1 as if it had not been notified that the stream pointer
has been repositioned to file beginning. How can I let Peek() work as
expected (i.e. return a value >=0 once the stream has been repositioned)?

Here's a sample code to reproduce my issue. Thx to all!

StreamReader sr = new StreamReader("c:\\work\\somefile.txt"))
Debug.Assert(sr.Peek()>-1); // OK
sr.ReadToEnd();
Debug.Assert(sr.Peek()==-1); // OK
sr.BaseStream.Seek(0L, SeekOrigin.Begin); // or
sr.BaseStream.Position = 0L;
Debug.Assert(sr.Peek()>-1); // ASSERTION FAILS!!!
Nov 16 '05 #1
1 18343
Dan <df***@hotmail.com> wrote:
Hi, I'm having a problem with StreamReader.Peek(). Let's say I open a file
and read it to end; then I'd want to move its stream pointer back to the
file beginning: I can call BaseStream Seek method or change the value of the
Position property, but this does not seem to affect the Peek() method, which
keeps returning -1 as if it had not been notified that the stream pointer
has been repositioned to file beginning. How can I let Peek() work as
expected (i.e. return a value >=0 once the stream has been repositioned)?


Use StreamReader.DiscardBufferedData. Basically, StreamReader often
reads more than you ask it to, keeping the read data in a buffer to
make things more efficient. DiscardBufferedData tells it that things
have changed, and it should ignore whatever it had already read.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: skibud2 | last post by:
Hi I am launching a process (I am running lisp) with the process class from my application. When I use the processName.StandardOutput.Peek() method, I get -1 even though I know there is data...
11
by: Tiger | last post by:
We can use seek() in the FileStream class,as we know. But I found that seek() is not work correctly in StreamReader. Who can tell me how to use seek() correctly in StreamReader? thanks a lot! I...
4
by: Astronomically Confused | last post by:
using System; using System.Collections; using System.IO; using System.Net; using System.Net.Sockets; using System.Threading; class HttpProcessor { private Socket s;
21
by: JoKur | last post by:
Hello, First let me tell you that I'm very new to C# and learning as I go. I'm trying to write a client application to communicate with a server (that I didn't write). Each message from the...
1
by: ATS | last post by:
HOWTO Make StreamReader class not block. Please help, I'm trying to use the Std-Out from a process I launch, to read back data from the process, until either the data I expcet is passed, or a...
4
by: | last post by:
I am receiving xml via tcp/ip and want to save it into a msmq queue. when I use the StreamReader.ReadLine() it only reads one line of the stream. but my xml could be more than 1 line. when I use...
1
by: Shawn | last post by:
Hi. I'm using this code to loop through all the lines in a text field: While myStreamReader.Peek() > -1 myStreamReader.ReadLine() i = i + 1 End While Now, what I need to do is to loop through...
3
by: pabelard | last post by:
I am reading from a file and trying to find out if it has characters above ASCII 127 in it. My sample file does have several of these characters. However, the streamreader seems to skip over...
3
by: Arpan | last post by:
A file can be read using only the StreamReader object like this: Dim sReader As StreamReader sReader = New StreamReader(Server.MapPath("File1.txt")) While(sReader.Peek -1)...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.