Discussion:
[fpc-pascal] How to append data to a file of byte?
Bo Berglund
2018-10-14 13:40:16 UTC
Permalink
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);
--
Bo Berglund
Developer in Sweden

_______________________________________________
fpc-pascal maillist - fpc-***@lists.freepascal.org
http://lists.freepascal.or
Michael Van Canneyt
2018-10-14 13:50:56 UTC
Permalink
Post by Bo Berglund
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?
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.
_______________________________________________
fpc-pascal maillist - fpc-***@lists.freepascal.org
http://lists.freepascal.org/
Bo Berglund
2018-10-14 15:53:03 UTC
Permalink
On Sun, 14 Oct 2018 15:50:56 +0200 (CEST), in
Post by Michael Van Canneyt
Post by Bo Berglund
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
Yes, I looked at that too, but it requires me to supply the current
size of the file as the second argument....

What will happen if the file does not exist yet? Do I have to test for
that too or will the Reset/Rewrite calls handle this?
Post by Michael Van Canneyt
Additionally, you must always open the file usng reset or rewrite.
SeekEOF (or Seek) when you didn't open the file first, is wrong.
Yes, I had not completed the code yet because of this issue with the
way to put the position at end of file...
Post by Michael Van Canneyt
For appending to an existing untyped file, you must call reset(), then call Seek.
Now I have changed to this, and it compiles without errors. But I
can't test yet because oteher parts of the code supplying the data is
not ready...

AssignFile(F, FileName);
if AppendToFile then
begin
Reset(F, 1);
Seek(F,FileSize(F)); // Does FileSize provide the size in bytes?
end
else
Rewrite(F,1);
BlockWrite(F, Buf[0], Length(Buf));
CloseFile(F);
Post by Michael Van Canneyt
In general, I would recommend using streams for binary IO.
I am pretty uncomfortable using streams. Don't really know how they
work...

(I am an old hand, started using Pascal on the Apple II, then T-Pascal
on IBM PC etc. That was back in the 1980:es...
Delphi since about 1995-96)
--
Bo Berglund
Developer in Sweden

_______________________________________________
fpc-pascal maillist - fpc-***@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/
Loading...