delphi 资料检测和重命名失败
delphi 文件检测和重命名失败
有以下一段代码:
procedure TTemplate.GetNewFile(FileName, AName, BName: string);
const
BufName = 'Buf.txt';
var F, Buf, N: TextFile;
S: string;
begin
AssignFile(F, FileName);
Reset(F);
AssignFile(N, BufName);
ReWrite(N);
while not EOF(F) do begin //把文件中所有AName的字符串替换为BName
Readln(F, S);
S := StringReplace(S, AName, BName, [rfReplaceAll]);
Writeln(N, S);
end;
CloseFile(F);
CloseFile(N);
if FileExists(FileName) then
DeleteFile(FileName);
ReNameFile(BufName, FileName);
end;
用于替换一个文件中的部分字符串,在执行到
if FileExists(FileName) then
DeleteFile(FileName);
ReNameFile(BufName, FileName);
FileExists、DeleteFile、ReNameFile都显示为inaccessible value
程序执行结果: 生成BufName.txt文件,但是重命名失败,无法替换源文件,源文件也没有执行删除
本人菜鸟,希望哪位大神帮忙看下,如果程序的逻辑有问题,顺便提下,谢谢
------解决方案--------------------
建议楼主直接使用TStringList
有以下一段代码:
procedure TTemplate.GetNewFile(FileName, AName, BName: string);
const
BufName = 'Buf.txt';
var F, Buf, N: TextFile;
S: string;
begin
AssignFile(F, FileName);
Reset(F);
AssignFile(N, BufName);
ReWrite(N);
while not EOF(F) do begin //把文件中所有AName的字符串替换为BName
Readln(F, S);
S := StringReplace(S, AName, BName, [rfReplaceAll]);
Writeln(N, S);
end;
CloseFile(F);
CloseFile(N);
if FileExists(FileName) then
DeleteFile(FileName);
ReNameFile(BufName, FileName);
end;
用于替换一个文件中的部分字符串,在执行到
if FileExists(FileName) then
DeleteFile(FileName);
ReNameFile(BufName, FileName);
FileExists、DeleteFile、ReNameFile都显示为inaccessible value
程序执行结果: 生成BufName.txt文件,但是重命名失败,无法替换源文件,源文件也没有执行删除
本人菜鸟,希望哪位大神帮忙看下,如果程序的逻辑有问题,顺便提下,谢谢
delphi
string
------解决方案--------------------
建议楼主直接使用TStringList
const
FileName = 'c:\aa.txt';
var
sLst : TStringList;
begin
sLst := TStringList.Create;
try
if not FileExists(FileName) then exit;
sLst.LoadFromFile(FileName);
sLst.Text := StringReplace(sLst.Text, 'a', 'b', [rfReplaceAll]);
sLst.SaveToFile(FileName);
finally
sLst.Free;
end;
end;