Reputation: 53
Given this program code:
CLS
OPEN "school.dat" FOR INPUT AS #5
DO WHILE NOT EOF(5)
INPUT #5,ID,N$,S$,PN$
IF LEFT$(N$,1)="A" OR LEFT$(N$,1)="a" THEN
PRINT ID,N$,S$,PN$
END IF
LOOP
CLOSE#5
END
I do not quite understand what is the use of do while not eof(5)
? What will happen if that eof(n)
syntax is removed?
Upvotes: 1
Views: 2132
Reputation: 1165
This snip describes how to use EOF function to exit file reading:
CLS
OPEN "school.dat" FOR INPUT AS #5
DO
IF EOF(5) THEN
EXIT DO
END IF
INPUT #5, ID, N$, S$, PN$
IF LEFT$(N$, 1) = "A" OR LEFT$(N$, 1) = "a" THEN
PRINT ID, N$, S$, PN$
END IF
LOOP
CLOSE #5
END
Upvotes: 0
Reputation: 130
EOF means end of file. Here in the program, the do loop continues until the file has not reached its end because of eof(5).If eof was removed the program would either go into a infinite loop or it would have been executed once only.
Upvotes: 1
Reputation: 3048
Also, to see the structure of your program more clearly, it helps to indent your code. So something like this:
cls
open"school.dat" for input as #5
do while not eof(5)
input #5,ID,N$,S$,PN$
IF LEFT$(N$,1)="A" OR "a" THEN
PRINT ID,N$,S$,PN$
END IF
LOOP
CLOSE#5
END
Upvotes: 1
Reputation: 76
It means that the loop will be running until it reaches end of the file, so that part of the code in the loop will repeat. You can also do it by "WHILE NOT EOF(5)" without "DO".
Upvotes: 1
Reputation: 2609
eof(5)
function EOF tests the file number passed to the function.
So basically its a loop that keeps a check and if end of file is encountered it will exit the loop. inshort, if you skip that bit, you wont be reading the file.
check this link and search for eof for an example https://www2.southeastern.edu/Academics/Faculty/pmcdowell/qbasic_manal.txt
Upvotes: 1
Reputation: 5220
it is saying, while you are not at the end of the file , do the code within the loop section.
Without it, the code will attempt to read from the end of the file and display an error message that it is past then end of the file
Upvotes: 1