473,395 Members | 1,578 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,395 software developers and data experts.

Using StreamReader.Peek()

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.Begin) 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 15471
You could also try the following:

myProgressBar.Value = 0
While myStreamReader.Peek <> -1
strLine = myStreamReader.ReadLine()
intPercent = (myStreamReader.BaseStream.Position * 100 ) /
myStreamReader.BaseStream.Length

'Do something with strLine here

'Update the progress bar
myProgressBar.Value += 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.Begin) but
myStreamReader.Peek() just returns -1.

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

myStreamReader.BaseStream.Position = 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.Value = 0
While myStreamReader.Peek <> -1
strLine = myStreamReader.ReadLine()
intPercent = (strLine.Length / myStreamReader.BaseStream.Length) * 100
'Do something with strLine here

'Update the progress bar
myProgressBar.Value += 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
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
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...
1
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...
6
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...
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...
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...
0
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...
2
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...
5
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
0
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...

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.