[fpc-pascal] possible bug in blockwrite

wkitty42 at windstream.net wkitty42 at windstream.net
Mon Mar 6 12:25:41 CET 2023


On 3/5/23 12:54 PM, Travis Siegel via fpc-pascal wrote:
[...]
> This reminded me that I used to do this exact thing under turbo pascal by using 
> blockread/writes, and treating the text file as a binary one. This allowed me to 
> make changes without having to rewrite the whole file, something even inifile 
> still does.
> 
> To make a long story short, I tried that technique in FPC, and was a bit 
> surprised by the results.

you don't say in your post which compels me to ask, what mode did you test in?

[...]
> blockwrite(f1,st1,6);
> 
> The reason it needs the extra byte is because the byte 0 (the one that stores 
> the size of the string) is also written to the file. TP did not behave this way, 
> though I don't know about delphi, though I'd doubt it dit.
> 
> It is fixable, one can simply return to the initial write location and write a 
> space character to remove the offending character.

that's not the proper fix, though... especially if you are looking for the 
number without a leading space... if it is at the beginning of the line, you 
wouldn't expect a leading space there, either...

but this is also very dependent on the actual type of the file... you said you 
did this with text files which i understand... they should not have the 
"housekeeping bits" and since you aren't defining the file as a "file of text", 
this cannot be automatically handled... instead the file is treated as a binary 
file and you have to manually handle removing the "housekeeping bits"...

FWIW: i've also done this for text files in a slightly different way... what was 
done was to create an index into the text file by basically counting all the 
lines... then if we wanted line 34, we would look up in the index where line 34 
starts and then seek there via the binary position of that line's beginning... 
then we would do whatever editing was needed and push or retract the line as 
needed... generally, for ease, we made our modifications on a temp file so we 
still had the original with its matching index and the temp file with its own 
index... so yeah, if we were changing line 34, we'd copy everything before it to 
the temp file, then copy 34 with the changes, and move on to the next set of 
changes to be done...

FWIW2: when the ability to use streams for text files came along, they were much 
easier to work with and we could concentrate on what needed to be done instead 
of the intricate details of how to do it... kind of like working with the 
various serial port forms... you either got up close and personal with each one 
or you used a general purpose library (aka FOSSIL driver) that knew how to talk 
to each one and you wrote your code the same for all and let the GP lib handle 
the actual "how" of talking to them...

-- 
  NOTE: No off-list assistance is given without prior approval.
        *Please keep mailing list traffic on the list where it belongs!*


More information about the fpc-pascal mailing list