[fpc-pascal] How to append data to a file of byte?
Michael Van Canneyt
michael at freepascal.org
Sun Oct 14 15:50:56 CEST 2018
On Sun, 14 Oct 2018, Bo Berglund wrote:
> It seems like I cannot use Append() on a file of byte.
> Neither is it allowed to use SeekEOF() on that file type...
>
> I want to log binary data to a logfile and therefore I need to open it
> for writing with record size =1 and the file pointer at the eof
> location.
> But all of the useful functions seem only to work with text files.
>
> What do I use to append binary data to an existing binary file?
>
> This throws an error at SeekEof:
>
> AssignFile(F, FileName);
> if AppendToFile then
> SeekEof(F)
> else
> Rewrite(F,1);
> BlockWrite(F, Buf[0], Length(Buf));
> CloseFile(F);
Was the Seek() function punished by you ? :)
https://www.freepascal.org/docs-html/rtl/system/seek.html
Additionally, you must always open the file usng reset or rewrite.
SeekEOF (or Seek) when you didn't open the file first, is wrong.
For appending to an existing untyped file, you must call reset(), then call Seek.
You can set filemode to influence how reset() opens the file for binary
files. Normally it is opened read-write..
https://www.freepascal.org/docs-html/rtl/system/filemode.html
Using reset may seem as a bit illogical, but this is an old TP-era inheritance.
In general, I would recommend using streams for binary IO.
Michael.
More information about the fpc-pascal
mailing list