AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

Untyped Binary Files

Ein Thema von sk.Silvia · begonnen am 19. Feb 2006 · letzter Beitrag vom 21. Feb 2006
Antwort Antwort
Seite 1 von 2  1 2      
Benutzerbild von sk.Silvia
sk.Silvia

Registriert seit: 8. Feb 2006
Ort: Slovenia
90 Beiträge
 
Delphi 7 Personal
 
#1

Untyped Binary Files

  Alt 19. Feb 2006, 17:11
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;
----------------------------------------------------------------------
  Mit Zitat antworten Zitat
mkinzler
(Moderator)

Registriert seit: 9. Dez 2005
Ort: Heilbronn
39.851 Beiträge
 
Delphi 11 Alexandria
 
#2

Re: Untyped Binary Files

  Alt 19. Feb 2006, 17:38
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.
Markus Kinzler
  Mit Zitat antworten Zitat
marabu

Registriert seit: 6. Apr 2005
10.109 Beiträge
 
#3

Re: Untyped Binary Files

  Alt 19. Feb 2006, 17:40
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
  Mit Zitat antworten Zitat
Benutzerbild von sk.Silvia
sk.Silvia

Registriert seit: 8. Feb 2006
Ort: Slovenia
90 Beiträge
 
Delphi 7 Personal
 
#4

Re: Untyped Binary Files

  Alt 19. Feb 2006, 17:59
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
  Mit Zitat antworten Zitat
Waldteufel
(Gast)

n/a Beiträge
 
#5

Re: Untyped Binary Files

  Alt 19. Feb 2006, 18:07
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: Whoops. I was wrong. It is no pointer.
  Mit Zitat antworten Zitat
marabu

Registriert seit: 6. Apr 2005
10.109 Beiträge
 
#6

Re: Untyped Binary Files

  Alt 19. Feb 2006, 18:13
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
  Mit Zitat antworten Zitat
Benutzerbild von sk.Silvia
sk.Silvia

Registriert seit: 8. Feb 2006
Ort: Slovenia
90 Beiträge
 
Delphi 7 Personal
 
#7

Re: Untyped Binary Files

  Alt 19. Feb 2006, 18:29
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...
  Mit Zitat antworten Zitat
marabu

Registriert seit: 6. Apr 2005
10.109 Beiträge
 
#8

Re: Untyped Binary Files

  Alt 19. Feb 2006, 18:52
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
  Mit Zitat antworten Zitat
Benutzerbild von sk.Silvia
sk.Silvia

Registriert seit: 8. Feb 2006
Ort: Slovenia
90 Beiträge
 
Delphi 7 Personal
 
#9

Re: Untyped Binary Files

  Alt 21. Feb 2006, 13:37
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???
  Mit Zitat antworten Zitat
marabu

Registriert seit: 6. Apr 2005
10.109 Beiträge
 
#10

Re: Untyped Binary Files

  Alt 21. Feb 2006, 14:14
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
  Mit Zitat antworten Zitat
Antwort Antwort
Seite 1 von 2  1 2      


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 15:26 Uhr.
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