函数:
procedure Convert_in_file(filename_nc
Char;filename_ct
Char;
needconvert:string;convertto:string);
var t1:TMemoryStream;
i:integer; //used as loop check
n1,n2:integer;
buf1:Word;
Str:string;
Str1,Str2,Str3:String;
file1:TextFile;
begin
t1:=TMemoryStream.Create;
t1.LoadFromFile(filename_nc);
n1:=length(needconvert);
for i:=0 to t1.Size do
begin
t1.read(buf1,1);
Str:=Str+Chr(buf1);
end;
n2:=Pos(needconvert,Str);
Delete(Str,n2,n1);
Insert(convertto,str,n2);
Form1.Memo1.Lines.Add(Str);
t1.Free;
Assignfile(file1,filename_ct);
Reset(file1);
Rewrite(File1);
Write(File1,Str);
Closefile(file1);
end;
调用:
Convert_in_file('my/source.txt','2.txt','$$title$$','hello');
其中source.txt为源文件
2.txt为目标文件
$$title$$为需要替换的部分
hello为替换后的部分
但结果的文本文件中出现莫名其妙的回车符号,望网友指点