Board index » delphi » Getting info from a binary file

Getting info from a binary file

Hello, I am trying to read a string[11] from a certain part in a file.  I know the hex address, but
how do I access it? I can read if it is the first byte in the file, but not anywhere after.  And how
do I declare a variable of a file that was made with another program?  TIA

In enlish it would be:

goto 0x0c410   {The hex postion}
read NAME      {The variable NAME which is a string[11]}

I tried

var
        NAME: string[11];
        SaveGameFile: file;
        FileName: string;

procedure GetStats;
begin
        FileName:='D:\orion\Save' + SaveGameNum + '.gam';
        assign(SaveGameFile, FileName);
        reset(SaveGameFile);
        {Gets the first ships stats}
        seek(SaveGameFile, $c410);
        read(SaveGameFile, NAME);        {Came up with an error --> SaveGameFile is an invalid file type}
end;

 

Re:Getting info from a binary file


You have to use BLOCKREAD (reading untyped files) instead
of READ (reading typed files)! And remember: Use RESET(file,recsize).
Recsize should be 1, except in special cases.

var NAME: string[11]; SaveGameFile: file; FileName: string;

procedure GetStats;
begin
  FileName:='D:\orion\Save' + SaveGameNum + '.gam';
  assign(SaveGameFile, FileName);
  reset(SaveGameFile,1); { ,1 means: Recordsize = 1 byte }
  seek(SaveGameFile, $c410);
  BLOCKread(SaveGameFile, NAME, SizeOf(Name));        
  Close(SaveGameFile);
end;

Re:Getting info from a binary file


1) For reading untypized file use BlockRead, not Read.
2) In Reset direct record size = 1
3) Variable for reading must be type of array[1..11] of char, not
string[11]. (In a string byte 0 used for it's length; first readed byte will
put in byte 0 of the string) :

var
NAME:string[11];
{-------} NAME2: array[1..11] of char;  { buffer for reading data }
SaveGameFile: file;
FileName: string;

procedure GetStats;
{------} var i:byte;
begin
FileName:='D:\orion\Save' + SaveGameNum + '.gam';
assign(SaveGameFile, FileName);
{------} reset(SaveGameFile,1);    { RecSize = 1 }
{Gets the first ships stats}
seek(SaveGameFile, $c410);
{-----} BlockRead(SaveGameFile, NAME2,1);
{-----} for i:=1 to 11 do Name[i]:=Name2[i];
{-----} Name[0]:=#11;         { Put Length of string = 11}
end;

  Harrynn.
harr...@glasnet.ru

Other Threads