8

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.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Skeela87
  • 701
  • 6
  • 12
  • 17

3 Answers3

12

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; 
Jørn E. Angeltveit
  • 3,029
  • 3
  • 22
  • 53
  • +1 I believe this is what he needs for school, the most basic db concept –  Apr 24 '11 at 03:58
  • Yes, that will do. Thank you very much. Very straightforward. – Skeela87 Apr 24 '11 at 05:28
  • Not really even a DB concept, just flat binary record persistence to disk. Perhaps the primogenitor, or earliest known way of doing binary persistence in the history of software development. This syntax goes all the way back to Wirth's pascal syntax, and was very very commonly used in the good old days, when I started using Turbo Pascal for DOS. – Warren P Apr 25 '11 at 02:37
5

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;
kludg
  • 27,213
  • 5
  • 67
  • 118
  • Thanks for the reply, however that makes little sense to me. It might also be a little too much for something in a school project. It's not important, I can do it how I can when I get back to school. – Skeela87 Apr 23 '11 at 10:30
  • 2
    +1 TStream is the most solid way to work with files, although not the easiest to understand for newcomers. – Jørn E. Angeltveit Apr 23 '11 at 10:44
  • Yeh, that's the problem. If I had time, I would happily learn it. But times not something I have a lot of at the moment unfortunately. – Skeela87 Apr 23 '11 at 10:46
  • Well, in addition to the `file of` method, you have the `TStringList` class with `SaveToFile` and `LoadFromFile` and the `TClientDataSet` component with XML import and export... – Jørn E. Angeltveit Apr 23 '11 at 11:19
1

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

Marco van de Voort
  • 25,628
  • 5
  • 56
  • 89
  • Ah thank you, I didn't know the correct term. I'll take a look and report back if this solves my problem or not. =) – Skeela87 Apr 23 '11 at 09:36