TFileStream

andrewyu
andrewyu used Ask the Experts™
on
How can I write line from a String to a File through Delphi 6 ?

Can you give me section of sample code ?

Andrew
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2008

Commented:
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.
Commented:
Found this on the newsgroups.
Regards
Smurff

procedure WriteLongString (FileName, st : string);

var    f    : tFileStream;

begin
f := tFileStream.Create (FileName, fmCreate);
try
    if st <> ''
    then f.Write (st [1], Length (st));
finally
    f.Free;
    end;
end;

procedure ReadLongString (FileName : string; var st : string);

var     f    : tFileStream;

begin
f := tFileStream.Create (FileName, fmOpenRead);
try
    SetLength (st, f.Size);
    if f.Size > 0
    then f.Read (st [1], f.Size);
finally
    f.Free;
    end;
end;
Top Expert 2008

Commented:
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.
Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Commented:
ziolko,

wouldnt you need to change your last example :

from

F.Read(str,lstr);

To

F.Write(str,lstr);

??
Top Expert 2008

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

Author

Commented:
Smurff,

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

Andrew
Top Expert 2004

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

meikl ;-)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial