Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Delphi中的简单读/写记录.dat文件_Delphi_File_Save_Record - Fatal编程技术网

Delphi中的简单读/写记录.dat文件

Delphi中的简单读/写记录.dat文件,delphi,file,save,record,Delphi,File,Save,Record,由于某种原因,我的OpenID帐户即使在昨天使用时也不再存在。但无论如何 我需要将记录数据保存到.dat文件中。我尝试了很多搜索,但都与数据库和BLOB相关。我无法从中构造任何东西 我有以下记录 type Scores = record name: string[50]; score: integer; end; var rank: array[1..3] of scores; 我只需要一种从.dat文件保存和读取记录数据的简单方法。我有一本关于如何

由于某种原因,我的OpenID帐户即使在昨天使用时也不再存在。但无论如何

我需要将记录数据保存到.dat文件中。我尝试了很多搜索,但都与数据库和BLOB相关。我无法从中构造任何东西

我有以下记录

   type
   Scores = record
     name: string[50];
     score: integer;
   end;  

var rank: array[1..3] of scores;

我只需要一种从.dat文件保存和读取记录数据的简单方法。我有一本关于如何做这件事的书,但那是在学校里。

在“块读”和/或“块写”下查看帮助。可能会有一个使用流的例子。下面是一个简单的演示(只是演示-实际上,不需要每次都重新打开文件流):


您还应该查看-方法的
文件

这有点过时了,但这是学习如何使用文件的一个好方法

由于具有动态数组(包括普通字符串)的记录无法使用此方法存储到文件中,因此不支持unicode字符串。但是
string[50]
是基于短字符串的,因此您的记录已经是非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; 
从文件读取

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; 

因此,您应该在类型前面加上
T
。例如,使用
TScores
作为类型名称,使用
Scores
作为变量名称。啊,谢谢,我不知道正确的术语。我将查看并报告这是否解决了我的问题。=)谢谢你的回复,不过这对我来说没什么意义。对于学校的项目来说,这也可能有点太多了。这并不重要,我可以在回到学校后尽我所能做到这一点。+1 TStream是处理文件最可靠的方法,尽管对新手来说不是最容易理解的。是的,这就是问题所在。如果我有时间,我会很高兴地学习它。但不幸的是,我现在没有太多的时间。好吧,除了
file of
方法之外,你还有
TStringList
类的
SaveToFile
LoadFromFile
以及
TClientDataSet
组件的XML导入和导出…+1我相信这就是他上学所需要的,最基本的db概念是的,可以。非常感谢你。非常简单。甚至不是DB概念,只是将二进制记录持久化到磁盘。也许是软件开发史上已知的最早的二进制持久化方法。这种语法可以追溯到Wirth的pascal语法,在我开始使用Turbo pascal for DOS的好日子里非常常用。