[Lazarus] read text file in lazarus

Christian Iversen chrivers at iversen-net.dk
Fri Apr 3 15:57:10 CEST 2009


Dians wrote:
> hi all, i have been watch a for a long time the growth up of lazarus , 
> i'm interesting but i'm still newbie.. how do i read text file in 
> lazarus ,  i have been looking for documentation but i have not still 
> get it yet, i have ever read like this :
> 
> lbIndexOpen := False;
> 
> while not lbIndexOpen do
>   begin
>     try
>       if FileExists(FileName) then
>         fStream := TFileStream.Create(FileName, fmOpenReadWrite)
> 
>       else
>         fStream := TFileStream.Create(FileName, fmCreate);
>       lbIndexOpen := True;
>     except
>       on e: Exception do
>         begin
>           WriteLn(E.Message);
>           Sleep(500);
> 
>         end;
>       end;
>   end; 
> 
> does that methode how to read text file ..?

This is not so good. In fact, it's a little bit insane ;-)

You are creating a TFileStream in every iteration of the loop, none of 
which are later freed. Also, there's a sleep(), which seems quite 
strange. Try this instead:

S := TStringList.Create();
S.LoadFromFile(Filename);
for X := 0 to S.Count-1 do
   writeln('Line ', X, ': ', S[X]);
S.Free();

-- 
Med venlig hilsen
Christian Iversen



More information about the Lazarus mailing list