How operate on TFileStream

10,595

Solution 1

If you need to read a text file and access each line, try instead using a TStringList class with this class you can load a file, read the data (accesing each line using a index) and save the data back.

something like this

FText  : TStringList;
i : integer;
begin
 FText := TStringList.Create;
 try
  FText.LoadFromFile('C:\Foo\Foo.txt');

    //read the lines
    for i:=0 to FText.Count-1 do    
     ProcessLine(FText[i]);  //do something   

  //Add additional lines
  FText.Add('Adding a new line to the end');
  FText.Add('Adding a new line to the end');    

  //Save the data back
  FText.SaveToFile('C:\Foo\Foo.txt');

 finally
  FText.Free;
 end;

end;

end;

Solution 2

I newer versions of Delphi you can use TStreamReader / TStreamWriter here is an example of using TStreamReader ... this is only for manipulating text files

var
  SR : TStreamReader;
  line : String;
begin
  SR := TStreamReader.Create('D:\test.txt');
  while not (SR.EndOfStream) do
  begin
    line := SR.ReadLine;
    ShowMessage(line);
  end;
  SR.Free;
 end;
Share:
10,595
Błażej
Author by

Błażej

Updated on June 27, 2022

Comments

  • Błażej
    Błażej almost 2 years

    Hello recently I replace TextFile with TFileStream. I never use it so I have small problem with it.

    • How can I add someting to my file after I assign it to variable?
    • How can I read someting form that file?

    I need defined line form that file so I was doing something like that:

    var linia_klienta:array[0..30] of string;
    AssignFile(tempPlik,'klienci.txt');
    Reset(tempPlik);
    i:=0;
    While Not Eof(tempPlik) do
      begin
        Readln(tempPlik,linia_klient[i]);
        inc(i);
      end;
    CloseFile(tempPlik);
    

    Then when line two is needed I simply

    edit1.text = linia_klienta[1];
    
  • Błażej
    Błażej over 12 years
    That looks nice... wait for it. Can I create new file?
  • OnTheFly
    OnTheFly over 12 years
    Using TStrings is very memory-inefficient.
  • RRUZ
    RRUZ over 12 years
    @Dudi, yes only get rid the line loadfromfile and then add new lines.
  • Arnaud Bouchez
    Arnaud Bouchez over 12 years
    @user539484 TStringList is not bad for normal text files size. In the OP question, it states [0..30] of string which is pretty nothing. In this case, TStringList is just efficient and easy to use.
  • Błażej
    Błażej over 12 years
    @PRUZ Sorry but it is not perfectly clear for me. When I want create new file how it look like?
  • Błażej
    Błażej over 12 years
    I'm using array of string. I need create new file/load from file, add new content to file.
  • Conrad Hildebrand
    Conrad Hildebrand over 12 years
    @Dudi: SaveToFile creates a new file, if it does not yet exist.
  • Błażej
    Błażej over 12 years
    But why when use 'SaveToFile' it overwrite exist content?
  • Błażej
    Błażej over 12 years
    @PRUZ there is any chace to save exist content?
  • RRUZ
    RRUZ over 12 years
    Are you trying of append data to this file? btw is RRUZ not PRUZ.
  • umlcat
    umlcat over 12 years
    I also suggest to use the TStringList class to manage several lines instead on a plain array. The problem here is how to read / write all those lines with a text / edit / memo control. The previous TStringList example already shows that.
  • OnTheFly
    OnTheFly over 12 years
    @Arnaud Bouchez, you have to define "normal" text file size first :-) BTW, OP reads to EOF, so N of lines might be more that just 31 (X < 62G-31 bytes)
  • RRUZ
    RRUZ over 12 years
    @Dudi, yes use the delete method passing the index of the line to remove FText.Delete(Index); btw if you have any addional question about this class fell free to ask(start) a new question in S.O.
  • Błażej
    Błażej over 12 years
    I know I should but i figure that out quickly with this