Author Topic: EOF is not reset when using seek  (Read 1697 times)

0 Members and 1 Guest are viewing this topic.

Offline davidf

  • Newbie
  • Posts: 6
EOF is not reset when using seek
« on: March 13, 2020, 02:15:49 PM »
for files open for input
if you reach the end of a file when searching the eof() is set to -1 (true) (as expected)
if then seek back to any position between 1 and less than the length of the file the eof() will still return -1
in qb eof() was reset by seeking to a position less than the length of file.

eof()  is only reset by reading some data  or by reopening the file

Anybody else noticed this, I could not see anything within the Search

Offline bplus

  • Forum Resident
  • Posts: 5896
  • B+ Knot again!
Re: EOF is not reset when using seek
« Reply #1 on: March 13, 2020, 02:51:31 PM »
I don't think you can do any seeking in INPUT file access mode, it is just sequential from start to end of file.

Are you thinking Binary Access?

Offline bplus

  • Forum Resident
  • Posts: 5896
  • B+ Knot again!
Re: EOF is not reset when using seek
« Reply #2 on: March 13, 2020, 03:05:43 PM »
Oh you can use seek in INPUT access, cool!
Code: QB64: [Select]
  1. ' test seek for INPUT file access
  2.  
  3. OPEN "Test SEEK data" FOR OUTPUT AS #1
  4. PRINT #1, "1234567"
  5. PRINT #1, "890"
  6. OPEN "Test SEEK data" FOR INPUT AS #1
  7. WHILE EOF(1) = 0
  8.     INPUT #1, fl$
  9.     PRINT fl$
  10. FOR i = 10 TO 1 STEP -1
  11.     SEEK 1, i
  12.     PRINT SEEK(1)
  13.  
  14.  

Offline bplus

  • Forum Resident
  • Posts: 5896
  • B+ Knot again!
Re: EOF is not reset when using seek
« Reply #3 on: March 13, 2020, 03:09:17 PM »
I don't think anyone would notice the EOF is still set to -1 because most use INPUT access to load a bunch of stored data into an array for faster access to file data.

Offline bplus

  • Forum Resident
  • Posts: 5896
  • B+ Knot again!
Re: EOF is not reset when using seek
« Reply #4 on: March 13, 2020, 03:15:50 PM »
Oh wait fooled by confusing i with data, seek doesn't work but neither is error thrown!

Code: QB64: [Select]
  1. ' test seek for INPUT file access
  2.  
  3. OPEN "Test SEEK data" FOR OUTPUT AS #1
  4. PRINT #1, "abcdefg"
  5. PRINT #1, "hij"
  6. OPEN "Test SEEK data" FOR INPUT AS #1
  7. WHILE EOF(1) = 0
  8.     INPUT #1, fl$
  9.     PRINT fl$
  10. FOR i = 10 TO 1 STEP -1
  11.     SEEK 1, i
  12.     PRINT SEEK(1)
  13. SEEK 1, 21
  14.  
  15.  
  16.  

Odd!
« Last Edit: March 13, 2020, 03:17:51 PM by bplus »

Offline Petr

  • Forum Resident
  • Posts: 1535
  • The best code is the DNA of the hops.
Re: EOF is not reset when using seek
« Reply #5 on: March 13, 2020, 04:57:33 PM »
It Interest me. If reading a file with INPUT stops 1 byte before the end of the file, then it works as you expect.

Code: QB64: [Select]
  1. ' test seek for INPUT file access
  2.  
  3. OPEN "Test SEEK data" FOR OUTPUT AS #1
  4. PRINT #1, "abcdefg"
  5. PRINT #1, "hij"
  6. PRINT #1, ""
  7. OPEN "Test SEEK data" FOR INPUT AS #1
  8. PRINT "SEEK>"; SEEK(1); "LOF>"; LOF(1)
  9. WHILE SEEK(1) < LOF(1) - 1
  10.     INPUT #1, fl$
  11.     PRINT fl$, SEEK(1), EOF(1)
  12. FOR i = 10 TO 1 STEP -1
  13.     SEEK 1, i
  14.     PRINT SEEK(1), EOF(1)
  15. SEEK 1, 21
  16. PRINT "------------------"
  17. FOR i = 10 TO 5 STEP -1
  18.     SEEK 1, i
  19.     PRINT SEEK(1), EOF(1)
  20.  

Offline davidf

  • Newbie
  • Posts: 6
Re: EOF is not reset when using seek
« Reply #6 on: March 14, 2020, 03:21:26 AM »
This behaviour caused some of my old programs to exit prematurely.
It took me a while to track down the error.
I would say it is a Bug
It is certainly causes different behaviour than QB programs

Offline bplus

  • Forum Resident
  • Posts: 5896
  • B+ Knot again!
Re: EOF is not reset when using seek
« Reply #7 on: March 14, 2020, 11:21:53 AM »
This behaviour caused some of my old programs to exit prematurely.
It took me a while to track down the error.
I would say it is a Bug
It is certainly causes different behaviour than QB programs

So SEEK works in old QB version? I don't think it did in QB45, INPUT access was pure sequential, that was the alternate name as I recall. Same with OUTPUT mode, it all had to be done sequentially.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1623
Re: EOF is not reset when using seek
« Reply #8 on: March 14, 2020, 02:46:40 PM »
Hi davidf
using the code of Bplus
Code: QB64: [Select]
  1. ' test seek for INPUT file access
  2.  
  3. OPEN "Test SEEK data" FOR OUTPUT AS #1
  4. PRINT #1, "1234567"
  5. PRINT #1, "890"
  6. OPEN "Test SEEK data" FOR INPUT AS #1
  7. WHILE EOF(1) = 0
  8.     INPUT #1, fl$
  9.     PRINT fl$
  10. FOR i = 10 TO 1 STEP -1
  11.     SEEK 1, i
  12.     PRINT SEEK(1)
  13.  
  14.  

I get in Qbasic under DOSBOX this result
 
and this help
  [ You are not allowed to view attachments ]  
while in QB64 I get this result
  [ You are not allowed to view attachments ]  .
It appears to work in the same manner!
« Last Edit: March 14, 2020, 02:48:26 PM by TempodiBasic »
Programming isn't difficult, only it's  consuming time and coffee

Offline TempodiBasic

  • Forum Resident
  • Posts: 1623
Re: EOF is not reset when using seek
« Reply #9 on: March 14, 2020, 02:49:10 PM »
errata corrige
QBASIC output
  [ You are not allowed to view attachments ]  
Programming isn't difficult, only it's  consuming time and coffee

Offline TempodiBasic

  • Forum Resident
  • Posts: 1623
Re: EOF is not reset when using seek
« Reply #10 on: March 14, 2020, 02:56:35 PM »
focusing more on the issue
Quote
if then seek back to any position between 1 and less than the length of the file the eof() will still return -1
in qb eof() was reset by seeking to a position less than the length of file.
also using this mod of Bplus code I get the same result from QB64 and Qbasic under Dosbox

Code: QB64: [Select]
  1. ' test seek for INPUT file access
  2.  
  3. OPEN "Test SEEK data" FOR OUTPUT AS #1
  4. PRINT #1, "1234567"
  5. PRINT #1, "890"
  6. OPEN "Test SEEK data" FOR INPUT AS #1
  7. FOR i = 1 TO 10
  8.     SEEK 1, i
  9.     PRINT SEEK(1)
  10. SEEK 1, 2
  11.  
  12. WHILE EOF(1) = 0
  13.     INPUT #1, fl$
  14.     PRINT fl$

Davidf please can you post some code to show the issue that you are describing?
Programming isn't difficult, only it's  consuming time and coffee

Offline bplus

  • Forum Resident
  • Posts: 5896
  • B+ Knot again!
Re: EOF is not reset when using seek
« Reply #11 on: March 14, 2020, 04:19:50 PM »
@TempodiBasic You haven't noticed SEEK is not working for OUTPUT file access mode? See my reply #4.
Code: QB64: [Select]
  1. FOR i = 10 TO 1 STEP -1
  2.     SEEK 1, i
  3.     PRINT SEEK(1)
  4.  

SEEK is just echoing i not actually finding anything in file.

Change this:
Code: QB64: [Select]
  1. OPEN "Test SEEK data" FOR OUTPUT AS #1
  2. PRINT #1, "1234567"
  3. PRINT #1, "890"
  4.  

to this:
Code: QB64: [Select]
  1. OPEN "Test SEEK data" FOR OUTPUT AS #1
  2. PRINT #1, "abcdefg"
  3. PRINT #1, "hij"
  4.  

And you'll see it immediately!
« Last Edit: March 14, 2020, 04:23:11 PM by bplus »

Offline TempodiBasic

  • Forum Resident
  • Posts: 1623
Re: EOF is not reset when using seek
« Reply #12 on: March 14, 2020, 06:55:01 PM »
Hi Bplus
Hi guys

I must affirm that with the previous codes and with this one below Ihave got the same results in QB64 and in QBasic!
So I think that QB64 is duplicating the behaviour of Qbasic.

Code: QB64: [Select]
  1. ' test seek for INPUT file access
  2. FOR k = 1 TO 2 STEP 1
  3.     OPEN "Test SEEK data" FOR OUTPUT AS #1
  4.     IF k = 1 THEN s1$ = "1234567": s2$ = "890" ELSE s1$ = "abcdefg": s2$ = "hij"
  5.     PRINT #1, s1$
  6.     PRINT #1, s2$
  7.     CLOSE #1
  8.     LOCATE 1
  9.     OPEN "Test SEEK data" FOR INPUT AS #1
  10.     FOR i = 1 TO 10
  11.         SEEK 1, i
  12.         IF k = 1 THEN LOCATE , 1 ELSE LOCATE , 10
  13.         PRINT SEEK(1)
  14.     NEXT
  15.     SEEK 1, 2
  16.  
  17.     WHILE EOF(1) = 0
  18.         INPUT #1, fl$
  19.         IF k = 1 THEN LOCATE , 1 ELSE LOCATE , 10
  20.         PRINT fl$
  21.     WEND
  22.     CLOSE #1
  23.     IF k = 1 THEN PRINT " Again......."

see here
  [ You are not allowed to view attachments ]  

  [ You are not allowed to view attachments ]  
  [ You are not allowed to view attachments ]  
IMHO the code works as aspected....
creating a file and  after reading from this
Code: QB64: [Select]
  prints on screen the position of the pointer into the sequential file opened as output
with
Code: QB64: [Select]
  1. SEEK 1,2
we set the pointer to the file at position 2nd byte from beginning
and in fact we got as output a first string starting from the 2nd byte "2" or "b"
and the loop
Code: QB64: [Select]
  1. DO WHILE EOF(1) = 0
activates no error of access behind the end of file!
So about 
Quote
if then seek back to any position between 1 and less than the length of the file the eof() will still return -1
in qb eof() was reset by seeking to a position less than the length of file.
it has no confirm in my experience.

But if I use code posted at #4 post by Bplus I find a difference between Qbasic and QB64....
see here
  [ You are not allowed to view attachments ]  

So the questions are:
1.
how can I SEEK numFile,Position behind the EOF? it is possible both in Qbasic both in QB64!

2.
why does the code posted in this post by me work? the SEEK 1,2 sets the position at 2nd byte and the reading of file starts there.

Here we need more data to fix the bug!

« Last Edit: March 14, 2020, 07:00:12 PM by TempodiBasic »
Programming isn't difficult, only it's  consuming time and coffee

Offline TempodiBasic

  • Forum Resident
  • Posts: 1623
Re: EOF is not reset when using seek
« Reply #13 on: March 14, 2020, 08:12:08 PM »
Sorry but following the pathway of Petr I have made my test code
Code: QB64: [Select]
  1. ' test seek for INPUT file access
  2. DIM s AS STRING * 1
  3. OPEN "Test SEEK data" FOR OUTPUT AS #1
  4. PRINT #1, "abcdefg"
  5. PRINT #1, "hij"
  6. OPEN "Test SEEK data" FOR INPUT AS #1
  7. PRINT "SEEK>"; SEEK(1); "LOF>"; LOF(1)
  8.     INPUT #1, fl$
  9.     PRINT fl$, SEEK(1), EOF(1)
  10. PRINT "EOF = "; EOF(1)
  11. PRINT " SEEK EOF ----- INPUT EOF"
  12. FOR i = 10 TO 1 STEP -1
  13.     s = "-"
  14.     SEEK 1, i
  15.     PRINT SEEK(1); "  "; s, i; EOF(1);
  16.     INPUT #1, s
  17.     LOCATE , 15
  18.     PRINT SEEK(1); " "; s; i; EOF(1)
  19.     LOCATE , 1
  20. SEEK 1, 21
  21. PRINT " SEEK after EOF"
  22. PRINT SEEK(1), EOF(1)
  23.  

running this code with QB64 and Qbasic I have got some differences that I post here like images
QB64    [ You are not allowed to view attachments ]  

and
QBasic
  [ You are not allowed to view attachments ]  

and we find different the EOF before and after the INPUT#1, s when i  is 10 and 9, moreover the variable s of 1 byte read from the last position in the file set by SEEK and the result is different when i = 9.
When i = 10 in both execution of program we reach the EOF by reading a byte (the character h), this happens also when i is 9 in Qbasic.

To solve his issue davidf can add a fake reading of file after setting pointer to the file in the middle of file and then read the data.


Programming isn't difficult, only it's  consuming time and coffee

Offline davidf

  • Newbie
  • Posts: 6
Re: EOF is not reset when using seek
« Reply #14 on: March 15, 2020, 05:00:20 AM »
Here is a sample bit of code that demonstrates the problem

Runs as expected in qb
Runs as expected in vbdos
Doesn't run as expected in qb64

 I cannot seem to insert a need code bloc (Sorry)
I have attached the eof_test.bas

'create test data file
OPEN "my_file.dat" FOR OUTPUT AS #1
FOR i% = 1 TO 5
    PRINT #1, "File_line_"; i%
NEXT
CLOSE #1

OPEN "my_file.dat" FOR INPUT AS #1

FOR i% = 1 TO 3

    SEEK #1, 1

    PRINT "Loop no:"; i%

    WHILE NOT EOF(1)
        LINE INPUT #1, filedata$
        PRINT filedata$
    WEND

NEXT

CLOSE #1

Have attached the output as well