Delphi-PRAXiS
Seite 1 von 2  1 2      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Object-Pascal / Delphi-Language (https://www.delphipraxis.net/32-object-pascal-delphi-language/)
-   -   Delphi Untyped Binary Files (https://www.delphipraxis.net/63488-untyped-binary-files.html)

sk.Silvia 19. Feb 2006 17:11


Untyped Binary Files
 
hi there everybody!

iam getting problems with this:(
the main thing is, by pressing Button1 i cretae, write in and read an untyped binary file, but when i press then button 3 i get an access violation (iam using the same read method!) WHY???

I understand also good german...:)

----------------------------------------------------------
Delphi-Quellcode:
procedure TForm1.Button1Click(Sender: TObject);
var MFile:file;
    var i1,tmp:integer;
  begin
  randomize;
  AssignFile(MFile,'cisla.bin');
  rewrite(MFile,SizeOf(integer));
  for i1:=1 to 25 do
    begin
    tmp:=random(500);
    BlockWrite(MFile,tmp,SizeOf(integer));
    Memo1.Lines.Append(IntToStr(tmp));
    end;

  reset(MFile,SizeOf(integer));
  while not eof(MFile) do
    begin
    BlockRead(MFile,tmp,SizeOf(integer));
    Memo2.Lines.Append(IntToStr(tmp));
    end;

  CloseFile(MFile);
  end;

procedure TForm1.Button3Click(Sender: TObject);
var MFile:File;
    tmp:integer;
  begin
  AssignFile(MFile,'cisla.bin');
 
  reset(MFile,SizeOf(integer));
  while not eof(MFile) do
    begin
    BlockRead(MFile,tmp,SizeOf(integer));
    Memo3.Lines.Append(IntToStr(tmp));
    end;

  closefile(MFile);
  end;
----------------------------------------------------------------------

mkinzler 19. Feb 2006 17:38

Re: Untyped Binary Files
 
BlockWrite and BlockRead needs a Pointer as second parameter, you use an integer. I'm not sure, but this is possibilly the reason for the access violation.

marabu 19. Feb 2006 17:40

Re: Untyped Binary Files
 
Hi Silvia,

it is good practice to close a file before reopening it - there is a CloseFile() missing in your OnClick handler for Button1. There seems to be no further problem with your code, means it performs as intended on my machine. Can you point out the very line of code that throws the exception?

BTW: Modern times file i/o is done with filestreams. You might want to read about it in the Online Help. You will transfer your knowledge about files quite easily. Typed and untyped files are ment to deal with legacy code.

kind regards

marabu

sk.Silvia 19. Feb 2006 17:59

Re: Untyped Binary Files
 
thanks for ideas

it dont points into any part of code, just the fallowing message after pressing button3:

Project Project1.exe raised eception class EAccessViolation with message 'Access violation at address 00000000. Read of address 00000000'. Process stoped. Use Step or Run to continue.

it dont helps to close the file before changing from write to read, also it dont helps to compleate recreate the file

i know about steam files and how to use them, but iam forced to use this solution:(

Waldteufel 19. Feb 2006 18:07

Re: Untyped Binary Files
 
Hi Silvia.

MKinzler already posted the solution.
You have to place an @-sign before tmp when passing it to BlockWrite and / or BlockRead, because they expect their second parameter being a pointer to a memory location where to put the value read from the file.

This should work:
Code:
BlockWrite(MFile, [color=#ff0000][b]@[/b][/color]tmp, SizeOf(integer));


//edit: :oops: Whoops. I was wrong. It is no pointer.

marabu 19. Feb 2006 18:13

Re: Untyped Binary Files
 
Access to address 0 usually happens due to an uninitialized pointer. Try to single step through Button3Click() just to make sure the exception really is thrown there. As the second parameter of BlockRead() is concerned you are doing alright. The procedure signature states the second parameter as an untyped variable, so you can pass in any variable type you like.

marabu

sk.Silvia 19. Feb 2006 18:29

Re: Untyped Binary Files
 
thanks

i made a bit investigation using ShowMessage steping

1) with pointer : when i add @ it writes me an error : variable required - but thats not the problem tmp without @ seems to be right

2) as i was tracking it, it looks like it dont handle eof(MFile) and make one more cycle which is fatal! - i have no clue why eof dont works (althrought it works right by button1 procedure)

Delphi-Quellcode:
showmessage('1');
  while not eof(MFile) do
    begin
    showmessage('2');
    BlockRead(MFile,tmp,SizeOf(integer));
    showmessage('3');
    Memo3.Lines.Append(IntToStr(tmp));
    showmessage('4');
    end;

  closefile(MFile);
  end;
i rewrited it to an until cycle, with no success:( i need to solve a bigger problem, but cant move when this dont works...

marabu 19. Feb 2006 18:52

Re: Untyped Binary Files
 
One more thing to mention is, you assign a block size when opening the file. You should make a decision:

Delphi-Quellcode:
// either
Reset(Mfile, 1);
BlockRead(Mfile, tmp, SizeOf(Integer));

// or
Reset(Mfile, SizeOf(Integer));
BlockRead(Mfile, tmp, 1);
In your case memory can get overwritten during a call to BlockRead(), even though my test didn't crash.

marabu

sk.Silvia 21. Feb 2006 13:37

Re: Untyped Binary Files
 
iam starting to get crazy :

Delphi-Quellcode:
procedure TForm1.Button1Click(Sender: TObject);
var MFile:file;
    var i1,tmp:integer;
  begin
  randomize;
  AssignFile(MFile,'cisla.bin');
  rewrite(MFile,1);
 
  for i1:=1 to 25 do
    begin
    tmp:=random(500);
    BlockWrite(MFile,tmp,1);
    Memo1.Lines.Append(IntToStr(tmp));
    end;

  //CloseFile(MFile);

  reset(MFile,1);
  while not eof(MFile) do
    begin
    BlockRead(MFile,tmp,1);
    Memo2.Lines.Append(IntToStr(tmp));
    end;

  CloseFile(MFile);
  end;


procedure TForm1.Button3Click(Sender: TObject);
var MFile:File;
    tmp:integer;
  begin
  AssignFile(MFile,'cisla.bin');
  reset(MFile,1);
 
  while not(eof(MFile)) do
    begin
    BlockRead(MFile,tmp,1);
    Memo3.Lines.Append(IntToStr(tmp));
    end;

  closefile(MFile);
  end;
i used a block in size 1 but OMG :
output by writing (Memo1)-procedure TForm1.Button1Click(Sender: TObject); :
17,357,77,42,55,109,80,383,443,142,66,248,109,11,3 00,299,453,37,344,10,497,9,411,124,295
output by reading (Memo2)-procedure TForm1.Button1Click(Sender: TObject); :
17,357,77,42,55,109,80,383,443,142,66,248,109,11,3 00,299,453,37,344,10,497,9,411,124,295
output by reading (Memo3)-procedure TForm1.Button3Click(Sender: TObject); :
4384017,4384101,4384077,4384042,4384055,4384109,43 84080,4384127,4384187,4384142,4384066,4384248,4384 109,4384011,4384044,4384043,4384197,
4384037,4384088,4384010,4384241,4384009,4384155,43 84124,4384039

why i get different outputs by using the same algorithm??? :wiejetzt:

marabu 21. Feb 2006 14:14

Re: Untyped Binary Files
 
Hi Silvia,

don't panic...

If you decide on a blocksize of 1 byte you must use a read / write count of SizeOf(Integer) - and everything falls in place.

Good Luck

marabu


Alle Zeitangaben in WEZ +1. Es ist jetzt 02:04 Uhr.
Seite 1 von 2  1 2      

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz