QB64.org Forum
Active Forums => QB64 Discussion => Topic started by: Richard 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.
-
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.
-
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...
-
Sorry, my mistake...
If I may reword my question to ...
In the following program, where data is appended to files$ -
[/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.
-
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.
-
Data can be written to the end of a file in binary mode too
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