delphi insert text into textfile at specific point

10,676

Solution 1

There are several ways you can do this.

The simplest version:

var 
  sl : TStringList;
  I : Integer;
begin
  sl := TStringList.Create;
  sl.LoadFromFile();
  // No you have an array of strings in memory one for each line
  for I := 0 to SL.Count -1 do
  begin
     sl.strings[I]; 
   end;
  sl.SaveToFile();
end;

You can also use other File IO Commands such as: To Read a text file:

  var
   t : TextFile;
  begin
    AssignFile(t,'FileName.txt');
    Reset(T);
    while not eof(t);
    begin
      Readln(T,LineOfText);
    end;
    CloseFile(T);
  end;

The to write out what you want..

  var
   t : TextFile;
  begin
    AssignFile(t,'FileName.txt');
    Rewrite(T);
    Writeln(T,LineOfText);
    Writeln(T,LineOfText);
    Writeln(T,LineOfText);
    CloseFile(T);
  end;

It should be noted that in reality both of the above methods are actually just rewriting the entire contents of the file.

The TFileStream class allows you manipulate the actual bytes of a file. In general you would need to read until you found the expected text. Then read ahead and cache the rest of the file, as you write out the new ending of the file.

Solution 2

var
  SL: TStringList;
  InsTextPos: Integer;
begin
  SL := TStringList.Create;
  try
    SL.LoadFromFile('c:\test.txt');
    InsTextPos := SL.IndexOf('//--begin inserting text here--//');
    if InsTextPos >= 0 then
    begin
      SL.Insert(InsTextPos+1, 'Inserting Line 2');
      SL.Insert(InsTextPos+1, 'Inserting Line 1');
      SL.SaveToFile('c:\test.txt');
    end;
  finally
    SL.Free;
  end;
end;
Share:
10,676
soulbrother
Author by

soulbrother

Updated on June 04, 2022

Comments

  • soulbrother
    soulbrother almost 2 years

    i want to edit a textfile. if i read a special line (let us say //--begin editing text here--//) then after this line i want to insert several lines but i don't want to override existing lines. is this possible with delphi? thanks!

    sample text:

    this

    is a file

    with text in it

    //--begin inserting text here--//

    and nothing in between

    sample text after edit:

    this

    is a file

    with text in it

    //--begin inserting text here--//

    now there is something

    in between

    and nothing in between

  • soulbrother
    soulbrother about 13 years
    how can i iterate the tstringlist line by line? it looks ok with the "indexof"-statement from robert but can i read line by line?
  • Roman Yankovsky
    Roman Yankovsky about 13 years
    for I := 0 to SL.Count-1 do { here do something with SL[I] };
  • David
    David about 13 years
    The old file IO methods you showed in your last two examples (AssignFile etc) are deprecated and there for backwards compatibility. (Very backwards, they've existed since the Turbo Pascal / DOS era.) You probably shouldn't recommend them. TFileStream or other streams are the 'normal' way to do file IO in Delphi.
  • Robert Love
    Robert Love about 13 years
    AssignFile may be old but it's not Deprecated. I have Delphi XE and just checked the system unit and help file and it is not marked as deprecated in any way.
  • David
    David about 13 years
    Ok, they're not marked with the deprecated keyword. It's still not the idiomatic technique in modern Pascal, and there's a perfectly good class that interacts with the WinAPI that does provides the same functionality much better. I'm trying to say I think these APIs are a bad example for someone who doesn't know much about Delphi - if you're going to expand past TStringList, it would be better to explain the idiomatic (and modern) technique instead.
  • Robert Love
    Robert Love about 13 years
    For appending to a text file, It works so well that I have never used anything else, and I am very familiar with TStream and all of the descendants.