Qbasicnews.com

Full Version: another question about files
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
How do i pull info out of the middle of a file like,

File to read:
1
3
4
5
2
87
4

How would i get the number on line 5?
Code:
open "numbers.txt" for input as #1
for i=1 to 5
line input 1,a$
next
print "Number at line 5 is";a$
close
is that the only way. reading all the lines up the the line you want then print it.... ok. thanks! [Image: KkatBigGrin.txt]

edit: how do you restart reasding from the begining of the file?
Another way would be to set up your file for random access. Then you read record #5 directly. The disadvantage is that now you no longer have a pure text file.

In Antoni's example, to start reading from the top of the file, you would have to close the file, and open it again. For a small file with few records, this is the simplest most straightforward way to process the file. The extra time involved for positioning to the record you want (reading up to it) is negligible.

The difference in speed mostly depends on whether you have a user waiting for the program's information. When a user is waiting, he is not going to notice a few milliseconds or even 1 to 3 seconds wait time. You can do a bunch of input's to a file in 1-3 seconds.
*****
SEEK #filenumber, 1

to reset the file pointer to the beginning of the file and then start reading over again from the beginning.
You could always load all the data of the file into an array (remember those)

so we would get something like...
Code:
open "numbers.txt" for input as #1
for i=1 to lengthofFile
   line input 1,a$(i)
next
print "Number at line 5 is";a$(5)
close


Doesn't SEEK only work with random access?
Quote:You could always load all the data of the file into an array (remember those)

so we would get something like...
Code:
open "numbers.txt" for input as #1
for i=1 to lengthofFile
   line input 1,a$(i)
next
print "Number at line 5 is";a$(5)
close


Doesn't SEEK only work with random access?

I did that and i used seek. so Seek works
"Doesn't SEEK only work with random access?"

(It's generally easier with random and binary files, though, because except for the beginning byte, byte locations in text files can be harder to track.)
Quote:How do i pull info out of the middle of a file like,

File to read:
1
3
4
5
2
87
4

How would i get the number on line 5?


tiger...why not just open as binary, calculate the position of the start of the 4th data chunk, then get the data? eg
(untested)
Code:
OPEN filein$ FOR BINARY AS #1
dataposition = (bytelengthofdatatype * datachunknumber) - 1  
REM eg if "5" is the 4th data chunk, and if datachunks are INT (2 bytes), then position of "5" is 4*2=8 END REM!!!
GET #1 , dataposition, variablename
CLOSE #1
one thing...I'm not sure if the first byte of a file is positioned at 0 or 1...you need to check this out and adjust occordingly

another thing to note...if the variable that you get is 2-byte, then the next GET will be advanced by 2-bytes...eg

GET #1, 1, x
GET #1, , x 'will get the data starting at position 3
One learns something every day.

I checked the manual and SEEK #filenumber,1 works with text files, binary files as well as random files. I haven't actually tested it yet.

There should definitely be a speed advantage to using the SEEK, because closing the file and re-opening it, is a slow process.
*****
Pages: 1 2