Results 1 to 9 of 9

Thread: Exceptions on binary reads

  1. #1

    Exceptions on binary reads

    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:
    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?
    Huhuhu

  2. #2
    Legendary Member cairnswm's Avatar
    Join Date
    Nov 2002
    Location
    Randburg, South Africa
    Posts
    1,537

    Exceptions on binary reads

    Have you looked into

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

    Not sur eif its what you want - but it may be.
    William Cairns
    My Games: http://www.cairnsgames.co.za (Currently very inactive)
    MyOnline Games: http://TheGameDeveloper.co.za (Currently very inactive)

  3. #3

    Exceptions on binary reads

    I normally do something like this:

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

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

    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.
    [size=9px]BEGIN GEEK CODE BLOCK
    <br />d s-- : a24 GB GCS GTW GE C++ P L+ W++ N+ K- w++++ M- PS+ PE+ Y- t+ 5+++ X+ R*
    <br />tv b+ DI++ D+ e++ h+ G-
    <br />END GEEK CODE BLOCK[/size]
    <br />Create your own GeekCode block at: <a href="">...</a>

  4. #4

    Exceptions on binary reads

    :shock: William you're fast!
    [size=9px]BEGIN GEEK CODE BLOCK
    <br />d s-- : a24 GB GCS GTW GE C++ P L+ W++ N+ K- w++++ M- PS+ PE+ Y- t+ 5+++ X+ R*
    <br />tv b+ DI++ D+ e++ h+ G-
    <br />END GEEK CODE BLOCK[/size]
    <br />Create your own GeekCode block at: <a href="">...</a>

  5. #5
    Legendary Member cairnswm's Avatar
    Join Date
    Nov 2002
    Location
    Randburg, South Africa
    Posts
    1,537

    Exceptions on binary reads

    Got to push up the post count you know - I might catch Traveller!
    William Cairns
    My Games: http://www.cairnsgames.co.za (Currently very inactive)
    MyOnline Games: http://TheGameDeveloper.co.za (Currently very inactive)

  6. #6

    Exceptions on binary reads

    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.
    [size=9px]BEGIN GEEK CODE BLOCK
    <br />d s-- : a24 GB GCS GTW GE C++ P L+ W++ N+ K- w++++ M- PS+ PE+ Y- t+ 5+++ X+ R*
    <br />tv b+ DI++ D+ e++ h+ G-
    <br />END GEEK CODE BLOCK[/size]
    <br />Create your own GeekCode block at: <a href="">...</a>

  7. #7

    Exceptions on binary reads

    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.
    Huhuhu

  8. #8
    Legendary Member cairnswm's Avatar
    Join Date
    Nov 2002
    Location
    Randburg, South Africa
    Posts
    1,537

    Exceptions on binary reads

    {$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
    William Cairns
    My Games: http://www.cairnsgames.co.za (Currently very inactive)
    MyOnline Games: http://TheGameDeveloper.co.za (Currently very inactive)

  9. #9

    Exceptions on binary reads

    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •