3

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
  • 57
  • 2
  • 2
  • 7

2 Answers2

8
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;
Roman Yankovsky
  • 1,207
  • 11
  • 23
3

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.

Robert Love
  • 12,447
  • 2
  • 48
  • 80
  • 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? – soulbrother Apr 19 '11 at 09:21
  • `for I := 0 to SL.Count-1 do { here do something with SL[I] };` – Roman Yankovsky Apr 19 '11 at 09:31
  • 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. – David Apr 20 '11 at 05:31
  • 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. – Robert Love Apr 20 '11 at 12:07
  • 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. – David Apr 21 '11 at 00:12
  • 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. – Robert Love Apr 21 '11 at 02:30