Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

vb.net progressbar max value for number of lines in a file

Posted on 2005-04-29
8
Medium Priority
?
631 Views
Last Modified: 2012-05-05
I have a file that I am reading and manipulating, I am not sure the maxium lines in the file, so is there a way to read how many lines are in a file first.


Right now I am looping a line at a time. Is there a file.linecount or anything I could use???
0
Comment
Question by:bman9111
8 Comments
 
LVL 5

Expert Comment

by:KeirGordon
ID: 13896865
Well there really is no such thing as 'lines' in the file, just a long string of characters (which may have newline characters).

If you are using a StreamReader, or similar, you can do StreamReader.basestream.length to see the total length.

Compare this to the Position property, and you can see the percentage of the file that has been read.

--KG

0
 
LVL 8

Author Comment

by:bman9111
ID: 13897332
yeah right now I have a file that looks like this


100 students
just in time
5000


what I was trying to do is create a progress bar that moved as it read each line, but having the progressbar be set at 3 for the max. Now keep in mind the max may be 12 , 100, 300.

thats why I wanted to see if it could could lines,

Any better alternatives

0
 
LVL 5

Expert Comment

by:KeirGordon
ID: 13897361
Well it is not possible to know the number of lines in the file without reading the entire file first.  

But for small files like you have I dont know why you wouldn't read the entire thing anyway, it would happen instantaneously, so the progress bar wouldn't serve much purpose.

You can read the entire file, then do a split() on the newline character to get an array of lines......

But really no way to read the number of lines faster than reading the entire file, unless of course you write the number of lines to the top of the file, the first line could be '3' on  your example file.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Author Comment

by:bman9111
ID: 13897553
well which is better.

dim lines as string
While filetoread.Peek <> -1
       lines = filetoread.readline
end while


or

dim i as integer
dim lines as string
dim linesplit () as string

lines = filetoread.readtoend
linesplit = line.split(vbcrlf)

for i = 0 to lines - 1
   msgbox linesplit(i)  
next


some textfiles will be really really big. Not sure what is better in memory, reading all at once or a line at a time.

where this is going if I readtoend then I can do a length and get how many lines, but I don't want to ruin performance if using peek is better
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 13897685
If your files will be "really really big" then reading the entire thing into a string and using split is not a good idea.  Reading line by line will take longer...but will not eat up lots of memory.
0
 
LVL 2

Accepted Solution

by:
RNMcLean earned 1050 total points
ID: 13899866
  Unfortunately, the modern way with files seems to have settled on them containing a stream of bytes only. The filesystem interface to your computer language on your system may offer some call that will tell you the file size in bytes (or whatever unit), and with a guess as to the average number of bytes/line you could make an estimate of the number of lines. Alternatively, your progress percentage could be based on (bytes read)/FileSizeInBytes*100% for an equivalent effect. Your accounting will need to be sure whether the end-of-record marker is included in the various counts. It may be CR, LF, or CRLF or LFCR, ha ha, but the question is whether the filesize includes them (probably does) and whether the READ statement's result does (probably doesn't), thus you need to add one, or maybe two to the bytes read of each input record. Be cautious as to the treatment of trailing spaces and tabs, etc. as well.
   Other file systems offer files with record lengths and know how many records there are in a file, so your requirement could be met directly.
0
 
LVL 1

Expert Comment

by:CyberSwine
ID: 13903892
Your application could look for another file (say your text file is named "DATA.TXT" then this other file could be called "DATA.LEN" or something) and if it exists, read the number of lines from it before reading in the lines. If this extra file is missing or older than the data file (in case some other program changed it), then your application would count the number of lines as it reads the file, and then create a new line count file. Whenever the data file is written to, the line count file should be updated.

This would speed up operation in the long run. Some professional programs use similar methods, eg. CoolEdit, which is an audio editor that stores some complementary data in a "peak file" so loading is faster. If the peak file is removed or the .WAV file changed by another program then loading is slower the next time (and a new peak file is created).

If you prefer seeking and reading fragments from the data file to reading the whole thing into memory, then you might even want to store a complete index of all the lines in your complementary file (which in this case I'll call the "index file"). If you use a long integer for each line position then the length of the index file (if divided by 4, the size of a long integer) will be the number of lines, and to get the position of a specific line you'd first do a seek in the index file to the line number (starting at 0) times four to find out where the line is. As with the line count file described above, whenever the index file is missing or the dates of the index file and data file don't match, and whenever you change the data file, you'd have to rebuild the index.

Hope this is of some help to you.
0
 
LVL 8

Author Comment

by:bman9111
ID: 13923224
so it the file.peek <> -1 a better way to read files???
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
Simple Linear Regression
Loops Section Overview

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question