The following code will open a file 'file.f' and read the first line
PRINT "The file first line"This will also show the first line, but what about the other lines.
OPEN "file.f" FOR INPUT AS #1
INPUT #1, ln$
PRINT ln$
CLOSE #1
A solution: You can think of this, looping through the lines of the file like that
OPEN "file.f" FOR INPUT AS #1However 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.
FOR i% = 1 TO 2
INPUT #1, ln$
PRINT ln$
NEXT i%
Luckily there's a function that will solve this problem!
EOF(file number) will
OPEN "file.f" FOR INPUT AS #1EOF return a boolean value (0 is false and -1 is true), if eof(1) = true then the loop stops
DO
INPUT #1, ln$
PRINT ln$
LOOP UNTIL EOF(1)
CLOSE #1
No comments:
Post a Comment