M
M
Maxim Markin2013-12-29 15:13:12
Delphi
Maxim Markin, 2013-12-29 15:13:12

How in Delphi to save data to a typed file, and also read information from it?

It is required to save data to a typed file, as well as read information from it.
Here is the record structure:

type
  phonebook=record
    firstname:string[255];
    lastname:string[255];
    phonenumber:string[255];
    adress:string[255];
end;

Well, the description:
var
  Form1: TForm1;
  pb:phonebook;
  F:File of phonebook;
  i,j,n,k:integer;

There is a save to file:
AssignFile(F, 'pbook.dat');
  Rewrite(f);
  n:=0;
    for i:=1 to StringGrid1.RowCount do
    begin
        pb.firstname:=StringGrid1.Cells[1,i];
        pb.lastname:=StringGrid1.Cells[2,i];
        pb.phonenumber:=StringGrid1.Cells[3,i];
        pb.adress:=StringGrid1.Cells[4,i];
      Write(F,pb);
      n:=n+1;
      Seek(F,n);
    end;
  CloseFile(F);

As well as reading and filling StrngGrid with data:
AssignFile(F, 'pbook.dat');
Reset(F);
n:=0;
while not eof(F) do
begin
  for i:=1 to StringGrid1.RowCount do
  begin
    with pb do
    begin
      Seek(F,n);
      Read(F,pb);
      StringGrid1.Cells[1,i]:=firstname;
      StringGrid1.Cells[2,i]:=lastname;
      StringGrid1.Cells[3,i]:=phonenumber;
      StringGrid1.Cells[4,i]:=adress;
      StringGrid1.RowCount:=StringGrid1.RowCount+1;
      inc(n);
    end;
  end;
end;

And the problem is that the filling does not work, and it seems to me that the reading is also, although I checked it without a cycle, manually, then everything was displayed in the textbox, but here it doesn’t want to. Also, throws an error on Read(F,pb); - unexpected end of file, although check is worth it. What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
M_PRO, 2013-12-29
@M_PRO

Why do Seek all the time? By the way records with 0 are numbered.
If memory does not change when working with records, Rewrite takes two values.
If there are headings then
In reading it is generally not clear what is written.
There are two cycles. And in one of them, an attempt to modify the counter limit (by the way, most likely will not work either). That is, if we have 10 records in the file, we count one as the first pass, two as the second, and so on, until the 11th pass, when we try to count 11 records, that is, more than there are in the file.
I would recommend looking at other people's examples.

A
Arman Toksimbaev, 2014-01-04
@toxicdream

Save to file:

AssignFile(F, 'pbook.dat');
  Rewrite(f);
  for i:=1 to StringGrid1.RowCount-1 do
  begin
      pb.firstname:=StringGrid1.Cells[1,i];
      pb.lastname:=StringGrid1.Cells[2,i];
      pb.phonenumber:=StringGrid1.Cells[3,i];
      pb.adress:=StringGrid1.Cells[4,i];
      Write(F,pb);
  end;
  CloseFile(F);

Why did you need n and Seek? Why go beyond the boundaries of StringGrid?
Reading:
AssignFile(F, 'pbook.dat');
  Reset(F);
  StringGrid1.RowCount := FileSize(F) + 1; // Точно не помню
  Seek(F, 0); // Может быть не нужен
  i := 1;
  while not eof(F) do
  begin
     with pb do
     begin
        Read(F,pb);
        StringGrid1.Cells[1,i]:=firstname;
        StringGrid1.Cells[2,i]:=lastname;
        StringGrid1.Cells[3,i]:=phonenumber;
        StringGrid1.Cells[4,i]:=adress;
        Inc(i);
      end;
    end;

Read more about loops and how they work. About the properties of objects and the numbering of arrays.
Learn to "read the code" - to scroll through the execution of the algorithm in your head.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question