Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2018-10

How to append data to a file of byte?

3 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

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?

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

Reply by email

Michael Van Canneyt
to Bo Berglund
Bo Berglund wrote 19 lines
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.

Reply by email

Bo Berglund
to Michael Van Canneyt

On Sun, 14 Oct 2018 15:50:56 +0200 (CEST), in

gmane.comp.compilers.free-pascal.general you wrote 13 lines
> 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

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?

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...

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);
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

Reply by email