Results 1 to 6 of 6

Thread: Saving a Dynamic Record Array to a file....

  1. #1

    Saving a Dynamic Record Array to a file....

    How can i save this dynamic record array quickly in delphi?

    [pascal]
    type
    tSinglePlayer = record
    Name : string;
    Numbers : array of integer;
    end;

    tPlayer : array of tSinglePlayer

    tDatabase = record
    Player : tPlayer;
    end;

    var
    Database : tDatabase;
    [/pascal]

    I previously used to use a simple piece of code to export the main database record.

    [pascal]
    var
    DatabaseFile : file of tDatabase;

    begin
    AssignFile(DatabaseFile, 'database.txt');
    Rewrite(DatabaseFile);
    writeln(DatabaseFile, Database);
    reset(DatabaseFile);
    end;
    [/pascal]
    However now that i have moved from static arrays to dynamic arrays, i get an error stating that 'tGame' needs finalization - not allowed in file type.

    NOTE: there is a lot more record types for each player than above and there could be up too 100,000 players...

    check out my game PAOL to see why, www.fresh-lobster.tk
    A lobster is for life not just for christmas.

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

    Saving a Dynamic Record Array to a file....

    A file of <type> must have fixed length records. Because its a dynamic array the file doesn;t know how large the records are going to be and therefore cannot allocate space on the disk for it.

    The best solution is to use two files.

    MainFile = File of Player Detail Record

    ChildFile = File of Recurring details record;

    Then the recurring detail record has a link id back to the primary record
    William Cairns
    My Games: http://www.cairnsgames.co.za (Currently very inactive)
    MyOnline Games: http://TheGameDeveloper.co.za (Currently very inactive)

  3. #3

    Saving a Dynamic Record Array to a file....

    The lazyman's solution to your problem:

    Use TCollections instead of arrays (your arrays are one-dimensional anyway).
    Then create a TComponent decendant with one of your collections as property and then you can just save that with TStream.SaveComponentRes

    The slightly more painful way would be to add a "Length" variable to all your records that contain an array and then to write the arrays to disk manually basically like this:

    Stream.Write(MyArray[I].SomeIntegerValue, SizeOf(Integer));
    Stream.Write(MyArray[I].Length, SizeOf(Integer));
    for J := 0 to Length(MyArray[I].SomeSubArray) - 1 do
    Stream.Write(MyArray[I].SomeSubArray[J].SomeValue, SizeOf(Integer);
    Ask me about the xcess game development kit

  4. #4

    Saving a Dynamic Record Array to a file....

    Ok but setting the length is not what i am intending to do. I need it to be anysize i want. Never liked streams anyway

    Would it be better to use a the database within delphi, if so any demonstration programs out there of one??
    A lobster is for life not just for christmas.

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

    Saving a Dynamic Record Array to a file....

    Consider using Access and the ADO components to link to it.

    I used to use a component called QDB (QuickDatabase) which was a record based database. Not sure if you'll find it anywhere though.
    William Cairns
    My Games: http://www.cairnsgames.co.za (Currently very inactive)
    MyOnline Games: http://TheGameDeveloper.co.za (Currently very inactive)

  6. #6

    Saving a Dynamic Record Array to a file....

    You need to have an untyped file if you want to save a record with dynamic content. The code would look something like this (untested):

    [pascal]
    procedure SaveRec(const P: TSinglePlayer);
    var
    len: Integer;
    f: File;
    begin
    AssignFile(f, MY_FILE_NAME);
    try
    Rewrite(f, 1);
    len := Length(P.Numbers);
    BlockWrite(f, len, SizeOf(len));
    BlockWrite(f, p.Numbers[0], SizeOf(P.Numbers[0]) * len);
    len := Length(P.Name);
    BlockWrite(f, len, SizeOf(len));
    BlockWrite(f, P.Name[0], SizeOf(P.Name[0]) * len);
    finally
    CloseFile(f);
    end;
    end;
    [/pascal]

    I'll let you play around with the code on your own now since I'm to tired to write anymore.
    Signature:
    <br />This is a block of text that can be added to posts you make. There is a 255 character limit

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
  •