Delphi中的简单读/写记录.dat文件 [英] Simple read/write record .dat file in Delphi

查看:115
本文介绍了Delphi中的简单读/写记录.dat文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某种原因,即使我昨天使用了 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...

写入文件

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆