PDA

View Full Version : Exceptions on binary reads



Smotsholle
17-07-2006, 09:13 AM
I'm working on a game project which makes use of binary files to store data. As I continue developing I want to add data to my records (and therefore the binary files), a logical consequence is that the program doesn't support the new type.

So if I try to load an obsolete binary data file the new record structure won't accept it, delphi shows me an error.
The fact that the old file doesn't work isn't a problem to me, but I want to make custom error handling. Of course I tried try-except but this doesn't seem to work.

Example code:

function LoadFromBinFile(filename:string):TMyRecord;
var myFile : File of TMyRecord;
begin
try
AssignFile(myFile, filename);
Reset(myFile);
Read(myFile, result);
CloseFile(MyFile);
except
ShowMessage(filename + ' has an obsolete file structure');
end;
end;

And eventhough I use a try...except the Read-file still gives me the delphi error. I must admit I don't know much about try...except in delphi, but there must be a way to catch this.

Does anyone know a solution to my problem?

cairnswm
17-07-2006, 09:43 AM
Have you looked into


{$I-}
OpenFile
{$I+}
If IOResult <> 0 then
Begin
End;


Not sur eif its what you want - but it may be.

tanffn
17-07-2006, 09:45 AM
I normally do something like this:

const
TFileVersion : array [0..19] of char = ( .... )
...

procedure LoadFile()
BlockRead( F, FileVersion, SizeOf(TFileVersion), dataRead );
if FileVersion <> TFileVersion then
begin
end;

TFileVersion is always the first thing I write to the file, it also has the nice advantage that when you look at the file with a simple text editor you’ll be able to read that text.

tanffn
17-07-2006, 09:48 AM
:shock: William you're fast! :)

cairnswm
17-07-2006, 09:54 AM
Got to push up the post count you know - I might catch Traveller!

tanffn
17-07-2006, 11:51 AM
Hey why do you think I’ve posted that "William you're fast!" post? :roll:
I think that after reaching to 1000 you’re entitled to slow down. :)

Smotsholle
17-07-2006, 12:21 PM
I've tried William's advice first and it worked!

Thanks for the advice.

Don't understand the {$I-} thingy, but if it gets the job done I'm not complaining.

cairnswm
17-07-2006, 01:06 PM
{$I-} - Turn off run time io error checking

{$I+} - turn it on again

IOResult - checks to see if an IO error happened.

Go check the help :)

jdarling
17-07-2006, 01:11 PM
As another way of doing it:

Rename your old structure to vXX_StrucName then create a new one. When loading use the previous code to test versions. Load the proper version, convert it up, write out new version. This pretty much lets the system self upgrade (as long as your careful). If you simply keep adding on to this, and making incremental update methods, you will find that any newer version can load any previous version. Course this is the reason I'm looking into streamed compression of XML files for storage of map info and the likes. Easy to read and decompress on the fly, also no upgrade paths unless major new features are added. As long as the compression format doesn't change and I don't completely re-name something older versions of the engine can load newer versions of the maps (might not always work as planned though) with nothing more then a warning displayed to the user.