2015-02-12 62 views
0

我做实验室工作,我需要通过一定的算法转换原始文件。这里是我的代码:为什么没有工作擦除和重命名?

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不重命名? 我只能使用顺序文件

+0

因为你的操作之前关闭这些文件。 – TLama 2015-02-12 08:01:30

+0

必须先关闭文件**,然后才能将其擦除或重命名(请参阅下面的答案)。 ''Erase()''和''Rename''是强制性的,不要打开它们。 – user1438038 2015-02-12 08:15:21

+1

@ user1438038,为true,但关闭*取消分配*。这是我的观点。 – TLama 2015-02-12 12:33:59

回答

2

请确保您的文件不被任何应用程序打开。从FreePascal文档:

擦除从磁盘中删除未打开的文件。应该为文件分配分配,但不打开复位或重写。

Program EraseDemo; 

Var MyFile: Text; 

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

    Erase(MyFile); 
end. 

重命名更改指定文件F S. ˚F必须分配的名字,但没有打开。

Program RenameDemo; 

Var MyFile: Text; 

begin 
    Assign(MyFile, paramstr(1)); 
    Rename(MyFile, paramstr(2)); 
end. 
相关问题