Author Topic: Most Reliable Code for EOF  (Read 2473 times)

0 Members and 1 Guest are viewing this topic.

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Most Reliable Code for EOF
« on: March 22, 2020, 10:30:52 am »
I have large program which Opens 50 files at once and attempts to input the data. The data files vary in length/quantity of data they hold. Seems I invariable run into the end of one of these files more often that not and I was wondering which of the following algorythms works the most reliably:

For x = 1 to 50
while NOT EOF (x)
Input #x, A,B,C

or
For x = 1 to 50
While EOF(x) = 0
Input #x, A,B,C

or
For x = 1 to 50
While EOF(x)<>-1

It's not really practical for me to put a dummy value at the end of each file.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Most Reliable Code for EOF
« Reply #1 on: March 22, 2020, 10:39:02 am »
You can't open and close them sequentially and store all their data in an array for faster access?

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: Most Reliable Code for EOF
« Reply #2 on: March 22, 2020, 11:33:14 am »
Not quite sure what you are asking here bplus. The 50 files are all open at once. The input #x, is in a DO loop which does read each one sequentially right to the end of the file. As each files' data is read, there are other routines (mostly calculations) which work on the data before the next open file is addressed. The data within each file is NOT stored in Arrays, and perhaps that could provide faster access but for now the accuracy of the calculations is my main concern.

You have me rethinking the error I'm running into. Every now and then I am running into an input past end of file which had me thinking I'm search for the wrong end of file marker, but maybe the problem is that all the files are not of equal length, or that I'm misreading the error code. Back to the drawing board.

Offline EricE

  • Forum Regular
  • Posts: 114
    • View Profile
Re: Most Reliable Code for EOF
« Reply #3 on: March 22, 2020, 12:39:34 pm »
To prevent going past the end of file you could you use LOC to give the current byte position in an open file and LOF to give the file size in bytes.
Then keep reading the file while LOC(filenum%) <= LOF(filenum%).

Edited to make the comparison "Less than or equal to".
« Last Edit: March 22, 2020, 02:55:02 pm by EricE »

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: Most Reliable Code for EOF
« Reply #4 on: March 22, 2020, 01:00:08 pm »
Thanks EricE, I'll give it a try.

Offline EricE

  • Forum Regular
  • Posts: 114
    • View Profile
Re: Most Reliable Code for EOF
« Reply #5 on: March 22, 2020, 02:54:31 pm »
You are welcome Dimster.
You probably should make the check "Less than or equal to" in order to get the last byte of the file.

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Most Reliable Code for EOF
« Reply #6 on: March 22, 2020, 09:50:53 pm »
DO UNTIL EOF(x)
LINE INPUT #x, A, B, C
LOOP
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Most Reliable Code for EOF
« Reply #7 on: March 22, 2020, 09:59:24 pm »
DO UNTIL EOF(x)
LINE INPUT #x, A, B, C
LOOP

+1
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Dimster

  • Forum Resident
  • Posts: 500
    • View Profile
Re: Most Reliable Code for EOF
« Reply #8 on: March 23, 2020, 10:53:34 am »
Thanks very much guys