473,566 Members | 2,770 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

StreamReader and Peek()

Dan
Hi, I'm having a problem with StreamReader.Pe ek(). 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\\somefi le.txt"))
Debug.Assert(sr .Peek()>-1); // OK
sr.ReadToEnd();
Debug.Assert(sr .Peek()==-1); // OK
sr.BaseStream.S eek(0L, SeekOrigin.Begi n); // or
sr.BaseStream.P osition = 0L;
Debug.Assert(sr .Peek()>-1); // ASSERTION FAILS!!!
Nov 16 '05 #1
1 18373
Dan <df***@hotmail. com> wrote:
Hi, I'm having a problem with StreamReader.Pe ek(). 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.Di scardBufferedDa ta. Basically, StreamReader often
reads more than you ask it to, keeping the read data in a buffer to
make things more efficient. DiscardBuffered Data tells it that things
have changed, and it should ignore whatever it had already read.

--
Jon Skeet - <sk***@pobox.co m>
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
1272
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 there. Has anyone had this happen to them Thank -Mike
11
35728
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 use the seek() like this: StreamReader r; ....... r.BaseStream.seek(.....);
4
8714
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
13056
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 server is on one line (\r\n at end) and is formed as - each of which is seperated by a space. Arguments with spaces in them are enclosed in...
1
2007
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 time limit hgas expired. My problem is that the StreamReader class blocks until the process writes something. Here is a snippet of code with the...
4
2995
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 StreamReader.ReadToEnd() it just listens to the port until the connection is closed and then it writes it to the queue, which is not the method that...
1
15509
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 all the lines one more time and do the work that I'm supposed to do. The reason for this is that I have to know how many lines exists in the text...
3
3789
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 them. Even if I read in lines and then write them out to another file, the high ASCII number characters disappear. My code:
3
3103
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) Response.Write(sReader.ReadLine) End While as well as using the FileStream object along with the StreamReader object like this:
0
7666
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7888
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8108
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7644
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7951
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6260
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5484
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2083
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.