这哪里难?你是懒吧!
下面的不是你需要的,参考一下吧!
...read line-by-line and modify a textfile ?
Author: Sebastian Derdulla
Homepage: http://move.to/sebspace
1 Comment to this tip [Write new comment]
[ Print tip ]
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 z do data:=inttostr(i)+' '+data;
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 z do writeln(f,data);
//save file and close it
CloseFile(f);
end;
end;