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?

Mailing lists / lazarus / 2008-12
8 messages · 3 people · –
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
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?
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")
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;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 :
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));Thanks Andrew.
In future I will look at using filestreams rather than go down the AssignFile route.
2008/12/19 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 :
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.