whats new ¦  programming tips ¦  indy articles ¦  intraweb articles ¦  informations ¦  links ¦  interviews
 misc ¦  tutorials ¦  Add&Win Game

Tips (1541)

Database (90)
Files (137)
Forms (107)
Graphic (114)
IDE (21)
Indy (5)
Internet / LAN (130)
IntraWeb (0)
Math (76)
Misc (126)
Multimedia (45)
Objects/
ActiveX (51)

OpenTools API (3)
Printing (35)
Strings (83)
System (266)
VCL (242)

Top15

Tips sort by
component


Search Tip

Add new Tip

Add&Win Game

Advertising

85 Visitors Online


 
...read line-by-line and modify a textfile?
Autor: Sebastian Derdulla
Homepage: http://move.to/sebspace
[ Print tip ]  

Tip Rating (17):  
     


procedure TForm1.Button1Click(Sender: TObject);
var
  
i, z: Integer;
  f: TextFile;
  t: string;
  Data: array of string;
begin
  if 
OpenDialog1.Execute then
  begin
    
//Read line by line in to the array data
    
AssignFile(f, OpenDialog1.FileName);
    Reset(f);
    z := 0;
    SetLength(Data, 0);
    //Repeat for each line until end of file
    
repeat
      
Inc(z);
      readln(f, t);
      SetLength(Data, Length(Data) + Length(t));
      Data[z] := t;
    until EOF(f);

    SetLength(Data, Length(Data) + 3 * z);
    //Add to each line the line number
    
for i := 1 to do Data[i] := IntToStr(i) + ' ' + Data[i];
    SetLength(Data, Length(Data) + 2);
    //Add a carriage return and line feed
    
Data[1] := Data[1] + #13 + #10;
    i       := Length(Data[5]);
    Data[5] := '';
    SetLength(Data, Length(Data) - i);
    //create a new textfile with the new data
    
AssignFile(f, OpenDialog1.FileName + '2');
    ReWrite(f);
    //write all lines
    
for i := 1 to do writeln(f, Data[i]);
    //save file and close it
    
CloseFile(f);
  end;
end;


 

Rate this tip:

poor
very good


Copyright © by SwissDelphiCenter.ch
All trademarks are the sole property of their respective owners