samrat luitel
samrat luitel

Reputation: 53

What is the purpose of "while eof(n)" in a qbasic program?

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

Answers (6)

eoredson
eoredson

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

therealyubraj
therealyubraj

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

BdR
BdR

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

NikolaTECH
NikolaTECH

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

Murlidhar Fichadia
Murlidhar Fichadia

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

lostbard
lostbard

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

Related Questions