Re: Delphi assign file

Posted by Joey *DClub* on July 26, 2004
In Reply to: assign file posted by keith on July 26, 2004:

Theres two ways you can do it. The method i would have done up until the other day when i found a new procedure i would have write all the content into a string. Added something to the end of the string then re-write the file with the contents of the new string. Example:

var
  F: TextFile;
  S1, S2, ExtraText: String;
begin
  ExtraText := 'Added to the end :)';
  AssignFile(F, 'C:\Documents\test.txt');
  Reset(F);
  while not Eof(F) do
  begin
    ReadLn(F, S1);
    S2 := S2 + S1 + #13#10;
  end;
  S2 := S2 + ExtraText;
  ReWrite(F);
  Write(F, S2);
  CloseFile(F);
end;

The second is probably a little more simpler it uses the append() procedure. Example:

var
  F: TextFile;
  ExtraText: String;
begin
  ExtraText := 'Added to the end :)';
  AssignFile(F, 'C:\Documents\test.txt');
  Append(F);
  WriteLn(F, ExtraText);
  CloseFile(F);
end;

The second aproach is a little easier to understand. Append does a similar thing to Reset() and ReWrite() procedures ;)

Joey ^__^


[ DelphiLand FAQ ]