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

发布于 2024-11-03 00:18:09 字数 337 浏览 4 评论 0原文

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

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

我有以下记录,

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

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

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

For some reason my OpenID account no longer exists even when I used it yesterday. But anyway.

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.

I have the following record

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

提笔书几行 2024-11-10 00:18:09

您还应该查看方法的文件

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

由于使用此方法无法将动态数组(包括普通字符串)的记录存储到文件中,因此不支持 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; 

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.

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; 

Read from file

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; 
最佳男配角 2024-11-10 00:18:09

使用流。这是一个简单的演示(只是演示 - 实际上不需要每次都重新打开文件流):

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

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

procedure WriteScores(var Buf; Count: Integer);
var
  Stream: TStream;

begin
  Stream:= TFileStream.Create('test.dat', fmCreate);
  try
    Stream.WriteBuffer(Buf, SizeOf(Scores) * Count);
  finally
    Stream.Free;
  end;
end;

procedure ReadScore(var Buf; Index: Integer);
var
  Stream: TStream;

begin
  Stream:= TFileStream.Create('test.dat', fmOpenRead or fmShareDenyWrite);
  try
    Stream.Position:= Index * SizeOf(Scores);
    Stream.ReadBuffer(Buf, SizeOf(Scores));
  finally
    Stream.Free;
  end;
end;

// write rank[1..3] to test.dat
procedure TForm1.Button1Click(Sender: TObject);
begin
  rank[2].name:= '123';
  WriteScores(rank, Length(Rank));
end;

// read rank[2] from test.dat
procedure TForm1.Button2Click(Sender: TObject);
begin
  rank[2].name:= '';
  ReadScore(rank[2], 2 - Low(rank));
  ShowMessage(rank[2].name);
end;

Use streams. Here is a simple demo (just demo - in practice there is no need to reopen file stream every time):

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

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

procedure WriteScores(var Buf; Count: Integer);
var
  Stream: TStream;

begin
  Stream:= TFileStream.Create('test.dat', fmCreate);
  try
    Stream.WriteBuffer(Buf, SizeOf(Scores) * Count);
  finally
    Stream.Free;
  end;
end;

procedure ReadScore(var Buf; Index: Integer);
var
  Stream: TStream;

begin
  Stream:= TFileStream.Create('test.dat', fmOpenRead or fmShareDenyWrite);
  try
    Stream.Position:= Index * SizeOf(Scores);
    Stream.ReadBuffer(Buf, SizeOf(Scores));
  finally
    Stream.Free;
  end;
end;

// write rank[1..3] to test.dat
procedure TForm1.Button1Click(Sender: TObject);
begin
  rank[2].name:= '123';
  WriteScores(rank, Length(Rank));
end;

// read rank[2] from test.dat
procedure TForm1.Button2Click(Sender: TObject);
begin
  rank[2].name:= '';
  ReadScore(rank[2], 2 - Low(rank));
  ShowMessage(rank[2].name);
end;
小…红帽 2024-11-10 00:18:09

查看“blockread”和/或“blockwrite”下的帮助。大概会有一个例子

Look in the help under "blockread" and or "blockwrite". There probably will be an example

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文