Sign up to the jQuery Grid Subscription list.

Monday, June 30, 2008

Reading a file with Qbasic

I'll pass quickly because I don't like to write long. First to do the remline.bas example (I even didn't try it but I want to create the code myself, without seeing the correction, some tries :) )
The following code will open a file 'file.f' and read the first line
PRINT "The file first line"
OPEN "file.f" FOR INPUT AS #1
INPUT #1, ln$
PRINT ln$
CLOSE #1
This will also show the first line, but what about the other lines.
A solution: You can think of this, looping through the lines of the file like that
OPEN "file.f" FOR INPUT AS #1
FOR i% = 1 TO 2
INPUT #1, ln$
PRINT ln$
NEXT i%
However using this solution you need to know the number of lines on the file, or an error will occur, or if you put an arbitrary number you may don't read all the lines. So we need a loop that stops when the file reach the end.
Luckily there's a function that will solve this problem!
EOF(file number) will
OPEN "file.f" FOR INPUT AS #1
DO
INPUT #1, ln$
PRINT ln$
LOOP UNTIL EOF(1)
CLOSE #1
EOF return a boolean value (0 is false and -1 is true), if eof(1) = true then the loop stops

No comments: