Arrays 从二进制文件读入记录数组
难以将二进制文件中的数据读入一个简单的记录数组,其中数组是固定的(通过常数=3)。我在论坛上寻找了一个解决方案,但毫无乐趣 以下是我的数组结构:Arrays 从二进制文件读入记录数组,arrays,pascal,Arrays,Pascal,难以将二进制文件中的数据读入一个简单的记录数组,其中数组是固定的(通过常数=3)。我在论坛上寻找了一个解决方案,但毫无乐趣 以下是我的数组结构: Const NoOfRecentScores = 3; Type TRecentScore = Record Name : String[25]; Score : Integer; End; TRecentScores = Array
Const NoOfRecentScores = 3;
Type
TRecentScore = Record
Name : String[25];
Score : Integer;
End;
TRecentScores = Array[1..NoOfRecentScores] of TRecentScore;
Var
RecentScores : TRecentScores;
这是我的程序,尝试从二进制文件加载3个分数
Procedure LoadRecentScores (var RecentScores:TRecentScores);
var MasterFile: File of TRecentScore;
MasterFileName: String;
count:integer;
Begin
MasterFileName:='HighScores.bin';
if fileexists(MasterFileName) then
begin
Assignfile(MasterFile, MasterFilename);
Reset(MasterFile);
While not EOF(MasterFile) do
begin
Read(Masterfile, RecentScores[count]); // issue with this line?
count:= count +1 ;
end;
Writeln(Count, ' records retrieved from file. Press ENTER to continue');
close(Masterfile);
end
else
begin
Writeln('File not found. Press ENTER to continue');
readln;
end;
end;
问题似乎与评论行有关…这里的问题是什么?当我编译并运行程序时,它意外退出 首次使用前,需要初始化
计数。(您可能还应该包括一个转义,以防止在获得的数据超过代码预期时从数组末尾运行。)
计数:=1;
while(不是EOF(MasterFile))和(Count)您应该注意,如果启用提示和警告,编译器将告诉您这类问题。在这种情况下,您可能会收到一条类似“警告:变量可能尚未初始化”的消息。如何启用它们取决于您使用的编译器和IDE;我在这台机器上没有Lazarus或FPC的副本,因此我不知道在哪里启用这些消息。
count := 1;
while (not EOF(MasterFile)) and (Count <= NoOfRecentScores) do
begin
Read(MasterFile, RecentScores[count];
Inc(Count); // or Count := Count + 1;
end;