473,794 Members | 2,748 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using StreamReader.Pe ek()

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 file in order to display a
ProgressBar to the user. I've tried setting
myStreamReader. BaseStream.Seek (0, SeekOrigin.Begi n) but
myStreamReader. Peek() just returns -1.

Is it not possible to achieve this without having to close the FileStream
and create a new one?

Thanks,
Shawn
Nov 20 '05 #1
1 15539
You could also try the following:

myProgressBar.V alue = 0
While myStreamReader. Peek <> -1
strLine = myStreamReader. ReadLine()
intPercent = (myStreamReader .BaseStream.Pos ition * 100 ) /
myStreamReader. BaseStream.Leng th

'Do something with strLine here

'Update the progress bar
myProgressBar.V alue += intPercent
End While

I actually tried this and its works fine.

Chris Dunaway wrote:
On 23 Apr 2004 14:30:22 +0200, Shawn wrote:
ProgressBar to the user. I've tried setting
myStreamReader. BaseStream.Seek (0, SeekOrigin.Begi n) but
myStreamReader. Peek() just returns -1.

You might try setting the Position property of the base stream to 0:

myStreamReader. BaseStream.Posi tion = 0

I don't know if that will work in this situation though.

Are the lines in the file all the same length? If so, you can get

the number of lines by dividing the length of the file by the line size.
Another method for handling a progress bar is to set the progress bar with a max value of 100. Then each time you read a line, take the length of the line and divide it by the length of the file and multiply by 100. Then increment the progress bar's value by that amount. That way you don't have to read through every line in the file beforehand:

'This is untested code
myProgressBar.V alue = 0
While myStreamReader. Peek <> -1
strLine = myStreamReader. ReadLine()
intPercent = (strLine.Length / myStreamReader. BaseStream.Leng th) * 100
'Do something with strLine here

'Update the progress bar
myProgressBar.V alue += intPercent
End While

Hope this helps a little

--
Chris

To send me an E-mail, remove the underscores and lunchmeat from my E-Mail address.


Nov 21 '05 #2

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

Similar topics

3
7186
by: Cheryl | last post by:
Hi! I am implementing a system that can read Chinese characters from a text file and do some string manipulations. Can anyone suggest a method on how I can do file I/O with Chinese? Thanks.
11
35801
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(.....);
1
18399
by: Dan | last post by:
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...
6
1897
by: syed javid | last post by:
hi all, I am getting an error when i process 1 MB txt file using StreamReader and i am reading char by char using streamReader.Read() method at some point it is throwing an exception System.ComponentModel.win32Exception:The handle is invalid. Any help for this problem or any kind of informaiton to process large files the file size can be 10 MB. thanks in advance
1
2018
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 problem: Process_MyProcess = new System.Diagnostics.Process();
3
3798
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:
0
927
by: rjjaw | last post by:
I need help with the streamreader. It appears that when I am reading the file that the streamreader jumps 3 lines before it is picked up by my variable again. Therefore I am only capturing a third of the information. Does anyone know why this is or how to help me get past this spot. Dim fs As FileStream Dim mystr As String Dim file As String Dim files() As String
2
8634
by: Al | last post by:
I'm currently attempting to use PLink (the console component of PUTTY - see http://www.chiark.greenend.org.uk/~sgtatham/putty/) as a Telnet component as I may in future need to change to using SSH and this seems an ideal solution. I'm running it as a process and re-directing the standardinput/output/error However, despite working through all the different variations of code I can either think of or find I am unable to achieve true...
5
2477
by: Kenny D | last post by:
I am having trouble sending streams of data from one socket to another using the following code. When the transmitter is running on the same machine as the receiver, the data transmits perfectly. When the transmitter runs on another machine, the data gets truncated at semi random places. The data being transmitted is a unicode XML string. There is nothing special about it other than being unicode.
0
9671
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9518
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10433
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10212
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10161
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5560
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4112
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
2
3720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.