Link to home
Start Free TrialLog in
Avatar of andrewyu
andrewyu

asked on

TFileStream

How can I write line from a String to a File through Delphi 6 ?

Can you give me section of sample code ?

Andrew
Avatar of Lukasz Zielinski
Lukasz Zielinski
Flag of Poland image

procedure TForm1.Button1Click(Sender: TObject);
var F: TextFile;
begin
  AssignFile(F,'sample.txt');
  Rewrite(F);
  try
    WriteLn(F,'line of string');
  finally
    CloseFile(F);
  end;
end;

ziolko.
ASKER CERTIFIED SOLUTION
Avatar of smurff
smurff

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
or if You want streams:

procedure TForm1.Button1Click(Sender: TObject);
var F: TFileStream;
    str: string;
    lstr: Integer;
begin
  str:='line of text';
  lstr:=Length(str);
  F:=TFileStream.Create('sample.txt',fmOpenWrite);
  try
    F.Read(str,lstr);
  finally
    F.Free;
  end;
end;

ziolko.
Avatar of smurff
smurff

ziolko,

wouldnt you need to change your last example :

from

F.Read(str,lstr);

To

F.Write(str,lstr);

??
smurff yeah my last example sucks, but it doesn't matter since Your is ok:-). just trying to make too many things at one time it always end with disaster:-)
ziolko.
Avatar of andrewyu

ASKER

Smurff,

It is almost ok, but, how can I write them LINE by LINE ?

Andrew
? use a tsringlist,
for its items (strings) tsringlist provides the methods
savetofile and  
loadfromfile

meikl ;-)