0

このコードを機能させようとしています。これは、標準の検索と置換機能です。

エラーはまったく発生しませんが、何らかの理由でテキスト ファイルに変更はありません。

完全なコードは次のとおりです。

procedure FileReplaceString(const FileName, searchstring, replacestring: string);
var
  fs: TFileStream;
  S: string;
begin
  fs := TFileStream.Create(FileName, fmOpenread or fmShareDenyNone);
  try
    SetLength(S, fs.Size);
    fs.ReadBuffer(S[1], fs.Size);
  finally
    fs.Free;
  end;
  S  := StringReplace(S, SearchString, replaceString, [rfReplaceAll, rfIgnoreCase]);
  fs := TFileStream.Create(FileName, fmCreate);
  try
    fs.WriteBuffer(S[1], Length(S));
  finally
    fs.Free;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
  var Path, FullPath:string;
begin
  Path:= ExtractFilePath(Application.ExeName);
  FullPath:= Path + 'test.txt';
  FileReplaceString(FullPath,'changethis','withthis');

end;
4

1 に答える 1