Sign in Join

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

Mailing lists / lazarus / 2008-12

Appending to a binary file

8 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

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

Reenen Laurie
to Dave Coventry

Yes...

"Seek" the end of the file, and then write to the file...

I think it's FileSeek these days.

(that's if you binary file is a "file of TMyRecord")

Dave Coventry wrote 9 lines
Can one do this? I know that you can append test to the end of Text files, but can you tack on data to an existing file? _______________________________________________ Lazarus mailing list http://www.lazarus.freepascal.org/mailman/listinfo/lazarus
o__ ,_.>/ _ (_)_\(_)_______ ...speed is good _______________ I believe five out of four people have a problem with fractions. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.lazarus.freepascal.org/pipermail/lazarus/attachments/20081219/d16cb147/attachment.html

Reply by email

Andrew Brunner
to Dave Coventry: Can one do this? I know that you can append test to the end of Text files, but can you…
procedure Append();
var
  sBuffer:string;
  iLength:integer;
begin
  sBuffer:='My Appended String'#13#10;
  iLength:=Length(sBuffer);
  FS:=TFileStream.Create(sFileName, fmshareDenyWrite);
  Try
    FS.Seek(0,soFromEnd);
    FS.WriteBuffer(sBuffer[1],iLength);
  Finally
    FreeAndNil(FS):
  end;
end;
Dave Coventry wrote 9 lines
Can one do this? I know that you can append test to the end of Text files, but can you tack on data to an existing file? _______________________________________________ Lazarus mailing list http://www.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email

Dave Coventry
to Reenen Laurie: Yes... "Seek" the end of the file, and then write to the file... I think it's FileSeek…

The file is type 'File'

var MF: File;
  bffer: array [0..127] of Byte;
begin
  AssignFile(MF,fname);
  FileMode:=fmOpenWrite;
  Reset(MF,1);
  Blockwrite(MF,bffer,128);
  CloseFile(MF);
end;

Do you reckon that would do it?

2008/12/19 Reenen Laurie :

Show quoted text 36 lines
Yes... "Seek" the end of the file, and then write to the file... I think it's FileSeek these days. (that's if you binary file is a "file of TMyRecord") On Fri, Dec 19, 2008 at 3:10 PM, Dave Coventry wrote: > > Can one do this? > > I know that you can append test to the end of Text files, but can you > tack on data to an existing file? > _______________________________________________ > Lazarus mailing list > > http://www.lazarus.freepascal.org/mailman/listinfo/lazarus -- o__ ,_.>/ _ (_)_\(_)_______ ...speed is good _______________ I believe five out of four people have a problem with fractions. _______________________________________________ Lazarus mailing list http://www.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email

Andrew Brunner
to Andrew Brunner: procedure Append(); var sBuffer:string; iLength:integer; begin sBuffer:='My Appended…

Ok, so if you want to use TFileStream you would just

FS.Seek(0,sofromEnd);
FS.Write(MyRec,SizeOf(MyRec));
Andrew Brunner wrote 29 lines
procedure Append(); var sBuffer:string; iLength:integer; begin sBuffer:='My Appended String'#13#10; iLength:=Length(sBuffer); FS:=TFileStream.Create(sFileName, fmshareDenyWrite); Try FS.Seek(0,soFromEnd); FS.WriteBuffer(sBuffer[1],iLength); Finally FreeAndNil(FS): end; end; On Fri, Dec 19, 2008 at 7:10 AM, Dave Coventry wrote: > Can one do this? > > I know that you can append test to the end of Text files, but can you > tack on data to an existing file? > _______________________________________________ > Lazarus mailing list > > http://www.lazarus.freepascal.org/mailman/listinfo/lazarus >

Reply by email

Dave Coventry
to Andrew Brunner

Thanks Andrew.

In future I will look at using filestreams rather than go down the AssignFile route.

2008/12/19 Andrew Brunner :

Show quoted text 45 lines
Ok, so if you want to use TFileStream you would just FS.Seek(0,sofromEnd); FS.Write(MyRec,SizeOf(MyRec)); On Fri, Dec 19, 2008 at 7:49 AM, Andrew Brunner wrote: > procedure Append(); > var > sBuffer:string; > iLength:integer; > begin > > sBuffer:='My Appended String'#13#10; > iLength:=Length(sBuffer); > > FS:=TFileStream.Create(sFileName, fmshareDenyWrite); > Try > FS.Seek(0,soFromEnd); > FS.WriteBuffer(sBuffer[1],iLength); > Finally > FreeAndNil(FS): > end; > end; > > On Fri, Dec 19, 2008 at 7:10 AM, Dave Coventry wrote: >> Can one do this? >> >> I know that you can append test to the end of Text files, but can you >> tack on data to an existing file? >> _______________________________________________ >> Lazarus mailing list >> >> http://www.lazarus.freepascal.org/mailman/listinfo/lazarus >> > _______________________________________________ Lazarus mailing list http://www.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email

Dave Coventry
to Andrew Brunner: procedure Append(); var sBuffer:string; iLength:integer; begin sBuffer:='My Appended…

Andrew (or anyone else), in the example you provided below, you instantiate the Filestream:

FS:=TFileStream.Create(sFileName, fmshareDenyWrite);

after which you use a try...finally block to write data to the file

 Try
    FS.Seek(0,soFromEnd);
    FS.WriteBuffer(sBuffer[1],iLength);
  Finally
    FreeAndNil(FS):
  end;

My question is: Does the FreeAndNil line free the stream? If I want to write again to the file, don't I need to instantiate the FS TFileStream object again using TFileStream.Create?

2008/12/19 Andrew Brunner :

Show quoted text 17 lines
procedure Append(); var sBuffer:string; iLength:integer; begin sBuffer:='My Appended String'#13#10; iLength:=Length(sBuffer); FS:=TFileStream.Create(sFileName, fmshareDenyWrite); Try FS.Seek(0,soFromEnd); FS.WriteBuffer(sBuffer[1],iLength); Finally FreeAndNil(FS): end; end;

Reply by email

Andrew Brunner
to Dave Coventry

Yes, if you've got a bunch of operations into this method write a wrapper class this way you can keep the stream alive until such time as it's not needed. Using FS.Seek(0 sofromEnd) or whatever you need to position the Stream.

Dave Coventry wrote 41 lines
Andrew (or anyone else), in the example you provided below, you instantiate the Filestream: FS:=TFileStream.Create(sFileName, fmshareDenyWrite); after which you use a try...finally block to write data to the file Try FS.Seek(0,soFromEnd); FS.WriteBuffer(sBuffer[1],iLength); Finally FreeAndNil(FS): end; My question is: Does the FreeAndNil line free the stream? If I want to write again to the file, don't I need to instantiate the FS TFileStream object again using TFileStream.Create? 2008/12/19 Andrew Brunner : > procedure Append(); > var > sBuffer:string; > iLength:integer; > begin > > sBuffer:='My Appended String'#13#10; > iLength:=Length(sBuffer); > > FS:=TFileStream.Create(sFileName, fmshareDenyWrite); > Try > FS.Seek(0,soFromEnd); > FS.WriteBuffer(sBuffer[1],iLength); > Finally > FreeAndNil(FS): > end; > end; _______________________________________________ Lazarus mailing list http://www.lazarus.freepascal.org/mailman/listinfo/lazarus

Reply by email