由于某种原因,即使我昨天使用过,我的 OpenID 帐户也不再存在。但不管怎么说。
我需要将记录数据保存到 .dat 文件中。我尝试了很多搜索,但都是与数据库和BLOB的东西有关。我无法从中构建任何东西。
我有以下记录
type
Scores = record
name: string[50];
score: integer;
end;
var rank: array[1..3] of scores;
我只需要一种简单的方法来保存和读取 .dat 文件中的记录数据。我有一本关于如何做到这一点的书,但那是在学校。
您还应该看看file of
-method.
这有点过时了,但它是学习如何使用文件的好方法。
由于使用此方法无法将具有动态数组(包括普通字符串)的记录存储到文件中,因此不支持 unicode 字符串。但string[50]
基于 ShortStrings,因此您的记录已经是非 unicode...
写入文件
var
i: Integer;
myFile: File of TScores;
begin
AssignFile(myFile,'Rank.dat');
Rewrite(myFile);
try
for i := 1 to 3 do
Write(myFile, Rank[i]);
finally
CloseFile(myFile);
end;
end;
从文件中读取
var
i: Integer;
Scores: TScores;
myFile: File of TScores;
begin
AssignFile(myFile, 'Rank.dat');
Reset(myFile);
try
i := 1;
while not EOF(myFile) do
begin
Read(myFile, Scores);
Rank[i] := Scores; //You will get an error if i is out of the array bounds. I.e. more than 3
Inc(i);
end;
finally
CloseFile(myFile);
end;
end;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)