Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2011-04

Read/Write files question

4 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Kjow

Hi all,

I have a problem with write and read binary files. I can't understand where I'm in wrong... I hope you can help me.

In a form I have 2 buttons, an edit and a memo.

In the source I made a simple record:

  TCustom1 = record
    Test1: integer;
    Test2: string;
  end;

and then I associated to relative ButtonClicks these codes:

procedure TForm1.Button1Click(Sender: TObject); //this save some data to a file
var
  i: integer;
  TestFile: File of TCustom1;
  Test: TCustom1;
begin
  AssignFile(TestFile,
ExtractFileDir(Application.ExeName)+'/'+Edit1.Text+'.tst');
  Rewrite(TestFile);
  for i:= 0 to 150 do
    begin
      Test.Test1:= i;
      Test.Test2:= ' -> test n? '+IntToStr(Test.Test1)+' <-';
      Write(TestFile, Test);
    end;
  CloseFile(TestFile);
end;
procedure TForm1.Button2Click(Sender: TObject); //this load data from a file
var
  i: integer;
  TestFile: File of TCustom1;
  Test: TCustom1;
begin
  i:= 0;
  Memo1.Clear;
  AssignFile(TestFile,
ExtractFileDir(Application.ExeName)+'/'+Edit1.Text+'.tst');
  Filemode:= fmOpenRead;
  Reset(TestFile);
  while not EOF(TestFile) do
    begin
      inc(i,1);
      Read(TestFile, Test);
      Memo1.Lines.Add('Row '+IntToStr(i)+'?) '+Test.Test2);
    end;
  CloseFile(TestFile);
end;

When I load the file, "i" has the right number of row (e.g. 150), but I get access violation.

I can't understand where I'm in wrong, if in write or in read or both... I already used file access, but it worked everytime well... now I can't make it working.

Thank you,
Kjow

PS Read/write could be used only with records or also with classes?

Reply by email

Sven Barth
to Kjow
Kjow wrote 14 lines
Hi all, I have a problem with write and read binary files. I can't understand where I'm in wrong... I hope you can help me. In a form I have 2 buttons, an edit and a memo. In the source I made a simple record: TCustom1 = record Test1: integer; Test2: string; end;

You must not use dynamic/managed types like strings, dynamic arrays, classes, etc. when using Read and/or Write. If you need strings then you must restrict yourself to ShortString. Otherwise you need to use TFileStream and read/write the file yourself.

[snip]

Show quoted text 12 lines
When I load the file, "i" has the right number of row (e.g. 150), but I get access violation. I can't understand where I'm in wrong, if in write or in read or both... I already used file access, but it worked everytime well... now I can't make it working. Thank you, Kjow PS Read/write could be used only with records or also with classes?

Only records as far as I'm aware of.

Regards,
Sven

Reply by email

Kjow
to Sven Barth

2011/4/4 Sven Barth :

You must not use dynamic/managed types like strings, dynamic arrays, classes, etc. when using Read and/or Write. If you need strings then you must restrict yourself to ShortString. Otherwise you need to use TFileStream and read/write the file yourself.

[cut]

Only records as far as I'm aware of. Regards, Sven

Oh!
Thank you, I'm very grateful for your help. This was a my bad lack.

Best regards,
Kjow

Reply by email

Sven Barth
to Kjow
Kjow wrote 13 lines
2011/4/4 Sven Barth: > You must not use dynamic/managed types like strings, dynamic arrays, > classes, etc. when using Read and/or Write. If you need strings then you > must restrict yourself to ShortString. Otherwise you need to use TFileStream > and read/write the file yourself. [cut] > Only records as far as I'm aware of. > > Regards, > Sven Oh! Thank you, I'm very grateful for your help. This was a my bad lack.

You are welcome.

Regards,
Sven

Reply by email