Author Topic: Behind the QB64 scenes - what happens when append to a file?  (Read 2729 times)

0 Members and 1 Guest are viewing this topic.

Offline Richard

  • Seasoned Forum Regular
  • Posts: 364
    • View Profile
Behind the QB64 scenes - what happens when append to a file?
« on: January 28, 2021, 10:26:43 pm »
When appending to a file eg

s$="a"
put #1,,s$   

where #1 is a previously setup BINARY file (eg suppose it is 1 Mbyte already)

Does QB64 completely save an updated version of the BINARY file (ie 1Mbyte + 1 byte) (and deletes the previous version i.e. 1MByte) or does it simply appends s$ to the physically existing BINARY file thereby at most writing the number of bytes defined by the File Allocation Unit (eg 1024 bytes for NTFS if set up that way).

Similarly, if the file #1 was set up as OUTPUT and RANDOM.

Offline luke

  • Administrator
  • Seasoned Forum Regular
  • Posts: 324
    • View Profile
Re: Behind the QB64 scenes - what happens when append to a file?
« Reply #1 on: January 28, 2021, 10:58:59 pm »
If the file is open AS BINARY then by definition you are not appending, as that would require opening the file AS APPEND.

Recall the difference of the access modes: http://www.qb64.org/wiki/OPEN#File_Access_Modes

Assuming a file opened AS BINARY, your listed operation can be considered an in place edit of existing content.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Behind the QB64 scenes - what happens when append to a file?
« Reply #2 on: January 28, 2021, 11:35:45 pm »
And if you’re asking where PUT places data, it’s at the last spot the file was accessed.

When you OPEN ..FOR BINARY.. the file pointer is at the first byte.
PUT a 10 byte string there, and the file pointer is now at byte 11.  (The file you put went from byte 1 to 10)

PUT #file, , data simply puts the data sequentially at the next byte.

SEEK #1, 123 <— moves the file pointer to the 123 byte
PUT #1, , a_long&  <— puts a long from byte 123 to byte 126.  The file pointer is now at byte 127.
PUT #1, , a_float##  <— now puts the float starting at byte 127...
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Richard

  • Seasoned Forum Regular
  • Posts: 364
    • View Profile
Re: Behind the QB64 scenes - what happens when append to a file?
« Reply #3 on: January 29, 2021, 04:48:45 am »
Sorry, my mistake...

If I may reword my question to ...

In the following program, where data is appended to files$ -


Code: QB64: [Select]
  1. files$ = "A:\file.txt"
  2. m$ = SPACE$(1000000&)
  3. s$ = "x"
  4. OPEN files$ FOR APPEND AS #1: PRINT #1, m$;: CLOSE #1 '             'DISPLAY'
  5. OPEN files$ FOR APPEND AS #1: PRINT LOF(1): PRINT #1, s$;: CLOSE #1 '1000000
  6. OPEN files$ FOR APPEND AS #1: PRINT LOF(1): PRINT #1, s$;: CLOSE #1 '1000001
  7. OPEN files$ FOR APPEND AS #1: PRINT LOF(1): CLOSE #1 '              '1000002
  8.  
  9.  
[/font]

Does QB64 completely save an updated version of FILE$ when CLOSE #1 occurs (ie 1Mbyte + 1 byte + ...) (and deletes the previous version i.e. 1MByte) or does it simply appends s$ to the physically existing FILE$ on disk thereby at most writing the number of bytes defined by the File Allocation Unit (eg 1024 bytes for NTFS if set up that way)?

Hope this is clearer.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Behind the QB64 scenes - what happens when append to a file?
« Reply #4 on: January 29, 2021, 08:12:09 am »
It only writes what you tell it to write.

FILES$ = "A:\file.txt"
IF _FILEEXISTS(FILES$) THEN SHELL "del " + FILES$
m$ = SPACE$(1000000&)
s$ = "x"
OPEN FILES$ FOR APPEND AS #1: PRINT #1, m$;: CLOSE #1 '             'DISPLAY'         <--Here, we write 1,000,000 bytes
OPEN FILES$ FOR APPEND AS #1: PRINT LOF(1): PRINT #1, s$;: CLOSE #1 '<-- Here, we only write 1, starting at byte 1,000,001
OPEN FILES$ FOR APPEND AS #1: PRINT LOF(1): PRINT #1, s$;: CLOSE #1 '<-- Here, we only write 1, starting at byte 1,000,002
END

You don't open the file and rewrite over it.  You simply append the data you're printing to the end of it.
 
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline NOVARSEG

  • Forum Resident
  • Posts: 509
    • View Profile
Re: Behind the QB64 scenes - what happens when append to a file?
« Reply #5 on: January 30, 2021, 12:22:47 am »
Data can be written to the end of a file in binary mode too

Quote
OPEN "test111" FOR BINARY AS #1
DIM a AS STRING
a = "ABC"

PUT #1, , a

CLOSE

OPEN "test111" FOR BINARY AS #1

SEEK #1, LOF(1) + 1

PUT #1, , a

CLOSE

Without the "+1" the last byte of the file will be overwritten
« Last Edit: January 30, 2021, 12:25:48 am by NOVARSEG »