Delphi中简单的读/写记录.dat文件 [英] Simple read/write record .dat file in Delphi
问题描述
由于某种原因,即使我昨天使用它,OpenID帐户也不再存在。但是无论如何,
For some reason my OpenID account no longer exists even when I used it yesterday. But anyway.
我需要将记录数据保存到.dat文件中。我尝试了大量的搜索,但它都与数据库和BLOB的事情有关。我不能从中构建任何东西。
I need to save record data into a .dat file. I tried a lot of searching, but it was all related to databases and BLOB things. I wasn't able to construct anything from it.
我有以下记录
type
Scores = record
name: string[50];
score: integer;
end;
var rank: array[1..3] of scores;
我只需要一个从.dat文件保存和读取记录数据的简单方法。我有一本关于如何做的书,但这是在学校。
I just need a simple way of saving and reading the record data from a .dat file. I had the book on how to do it, but that's at school.
推荐答案
你还应该看看文件
-method。
You should also take a look at the file of
-method.
这是一个过时的,但这是一个很好的方法来学习如何处理文件。
This is kinda out-dated, but it's a nice way to learn how to work with files.
由于使用动态数组(包括普通字符串)的记录无法使用此方法存储到文件中,因此不支持unicode字符串。但是, string [50]
基于ShortStrings,因此您的记录已经是非unicode ...
Since records with dynamic arrays (including ordinary strings) can't be stored to files with this method, unicode strings will not be supported. But string[50]
is based on ShortStrings and your record is therefore already non-unicode...
写入文件
Write to file
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;
这篇关于Delphi中简单的读/写记录.dat文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!