Post by tsh73 on Nov 7, 2023 9:23:15 GMT
(this occured while trying to write PDF file)
1. If you write stuff/object in a file and you need byte offset from the start to this object (to use later),
you can store loc() before writing that object.
That will be needed byte offset.
2. sometimes you need to write data you will not have yet
(but it will be avalable some time later, or may be then whole file is written).
Then you can
* save position with saved_position=loc(#1)
* write a filler of known size instead of number you do not have
* proceed writing
** then number get calculated,
** save current position with current_position=loc(#1)
** reset curren tosition to saved one with seek #1, saved_position
** write your number over filler.
*** Use using() to get exact number of places
*** end write with ( ; ) so no extra CRLF got written
** restore current position
* proceed writing with seek #1, current_position
1. If you write stuff/object in a file and you need byte offset from the start to this object (to use later),
you can store loc() before writing that object.
That will be needed byte offset.
2. sometimes you need to write data you will not have yet
(but it will be avalable some time later, or may be then whole file is written).
Then you can
* save position with saved_position=loc(#1)
* write a filler of known size instead of number you do not have
* proceed writing
** then number get calculated,
** save current position with current_position=loc(#1)
** reset curren tosition to saved one with seek #1, saved_position
** write your number over filler.
*** Use using() to get exact number of places
*** end write with ( ; ) so no extra CRLF got written
** restore current position
* proceed writing with seek #1, current_position
open "test2.dat" for output as #1
#1 "%PDF-1.7"
print "Current position is ";loc(#1)
print " should be (10)"
'suppose I do not have (6) for next line, yet
'split problem string in three, print via (;) so no CRLF
'3 0 obj<</Type /Page /Parent 2 0 R /Resources 4 0 R /MediaBox [0 0 500 800] /Contents 6 0 R>>
#1 "3 0 obj<</Type /Page /Parent 2 0 R /Resources 4 0 R /MediaBox [0 0 500 800] /Contents ";
savedPos=loc(#1)
filler$="###"
#1 filler$;
#1 " 0 R>>"
print "second line printed as:"
print "3 0 obj<</Type /Page /Parent 2 0 R /Resources 4 0 R /MediaBox [0 0 500 800] /Contents ";
print filler$;
print " 0 R>>"
#1 "endobj"
'now print some stuff
#1 "absdefgh"
#1 "ijklmnop"
#1 "revind back and fix second line"
'suppose I got that number
contentsObjNum=6
'replace filler with a number
curPos=loc(#1)
seek #1, savedPos
#1, using("###",contentsObjNum);
'then continue printing
seek #1, curPos
#1 "continue here"
#1 "qrstuwxyz"
#1 "%%EOF"
close #1
print "file 'test2.dat' is written"
print "second line should contain"
print tab(10), "/Contents 6 0 R"
run "notepad.exe test2.dat"
input "Press ENTER to quit";a$