在ADA83中循环

use*_*894 0 file-io ada while-loop

我在Ada83中编写了以下代码来查找文件的大小:

with Text_IO;
use Text_IO;

procedure imp3ada is

package Int_IO is new Text_IO.Integer_IO(Integer);
use Int_IO;

inputFile: File_Type;
i: integer:=0;
fileSize: Integer:=0;
fileCurrentPosition: Integer:=0;

begin
    open(inputFile, In_File, "test.cfg");
        while not End_of_File(inputFile) loop
        fileCurrentPosition:=fileCurrentPosition+1;
        end loop;
    fileSize:=fileCurrentPosition;
    Int_IO.put(fileCurrentPosition);
    Int_IO.put(fileSize);
    close(inputFile);
end imp3ada;
Run Code Online (Sandbox Code Playgroud)

代码编译得很好,但是当我尝试运行时没有任何反应,我希望它能够打印文件中的字符数.我猜它会进入无限循环.有人可以帮助我解决它出错的地方吗?

tra*_*god 5

如果要以任意单位查找文件的大小,可以Direct_IO使用某种合适的类型进行实例化并调用该SIZE函数.概括地说,

with Direct_IO;
…
package DIO is new Direct_IO(Character);
F: DIO.File_Type;
Size: Integer;
…
DIO.Open(F, DIO.In_File, Name);
Size := Integer(DIO.Size(F));
DIO.Close(F);
Run Code Online (Sandbox Code Playgroud)

附录:另见

附录:我应该为简单的配置文件编写解析器.

对于解析配置,您可以读取该文件Line_By_Line.这样,您无需事先知道文件的长度.获得a后Line,可以对组件字符编制索引,如§3.6.3所示.类型字符串.