为什么擦除和重命名不起作用?

Why didn't work the erase and rename?

我做实验室工作,需要通过一定的算法转换原始文件。这是我的代码:

var f1,f2: text;

procedure rounds(var f1, f2: text);
  var a: real;
  begin
    while not EoF(f1) do
      begin
        read(f1, a);
        write(f2, a:0:1, ' ');
      end;
  end;

begin
  assign(f1, './lab.txt');
  reset(f1);
  assign(f2, './temp'); rewrite(f2);
  rounds(f1,f2);
  close(f1);
  close(f2);
  Erase(f1);
  rename(f2, 'lab.txt');
end.

为什么f1不删,f2不改名? 而且我只能使用顺序文件

确保您的文件没有被任何应用程序打开。来自 FreePascal 文档:

Erase removes an unopened file from disk. The file should be assigned with Assign, but not opened with Reset or Rewrite.

Program EraseDemo;

Var MyFile: Text;

begin
  Assign(MyFile, 'demo.txt');
  Rewrite(MyFile);
  Writeln(MyFile, 'Lorem Ipsum dolor est');
  close (MyFile);

  Erase(MyFile);
end.

Rename changes the name of the assigned file F to S. F must be assigned, but not opened.

Program RenameDemo;

Var MyFile: Text;

begin
  Assign(MyFile, paramstr(1));
  Rename(MyFile, paramstr(2));
end.