Recent

Author Topic: Writing Record structure to disk and retrieving it  (Read 1492 times)

rwebb616

  • Full Member
  • ***
  • Posts: 133
Writing Record structure to disk and retrieving it
« on: April 30, 2021, 04:45:38 am »
Hello, I have the following unit:
Code: Pascal  [Select][+][-]
  1. unit scores.util.structures;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils,Forms;
  9.  
  10. Type
  11.   TLabelProperties = Record
  12.     Top : integer;
  13.     Left : integer;
  14.     //Color : Tcolor;
  15.     Size : integer;
  16.     Font : String[50];
  17.   end;
  18.  
  19.   Toptions=Record
  20.     filename:string[150];        // Options filename
  21.     sbmonitor:integer;      // Scoreboard Monitor - display scoreboard on this monitor number
  22.     backgrounds:TStringList; // a list of all added background filenames
  23.     LastBackground : string[150]; // Last background used
  24.     NumberOfTeams : integer; // number of teams to track
  25.     TeamLabels : Record
  26.       Team : array [1..4] of TLabelProperties;
  27.     end;
  28.   end;
  29.  
  30. var
  31.   options : TOptions;
  32.  
  33. implementation
  34. end.

options is used only as a single record for handling the application variables.  I was thinking I could write this structure to disk relatively easily with this:
Code: Pascal  [Select][+][-]
  1. procedure SaveSettings;
  2. var
  3.   F : file of TOptions;
  4.   //INI : Tinifile;
  5.   //i : integer;
  6. begin
  7.   // save settings to dat file for testing
  8.   Assign (F,'options.dat');
  9.   ReWrite(F);
  10.   write (F,options);
  11.   close(F);
  12. end;

And read it back in with this:
Code: Pascal  [Select][+][-]
  1. procedure LoadSettings;
  2. var
  3.   F : file of Toptions;
  4.   //INI : Tinifile;
  5.   //SL : Tstringlist;
  6.   //i,loc : integer;
  7. begin
  8.   Assign (F,'options.dat');
  9.   reset(F);
  10.   read(F,options);
  11.   close(F);
  12. end;

But it's not appearing to write out everything and I'm not sure why - is it because I have an embedded record inside Toptions?
Rich

lucamar

  • Hero Member
  • *****
  • Posts: 4014
Re: Writing Record structure to disk and retrieving it
« Reply #1 on: April 30, 2021, 05:24:55 am »
Can you determine what is it not writing? It's probably the string list (the rest, even the record, should be written a'right), but better make sure ...
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Handoko

  • Hero Member
  • *****
  • Posts: 4291
  • My goal: build my own game engine using Lazarus
Re: Writing Record structure to disk and retrieving it
« Reply #2 on: April 30, 2021, 05:29:04 am »
The content of the TStringList.

The type of Backgrounds is TStringList, the variable is a pointer type variable so it writes only the address where it points to.

Bart

  • Hero Member
  • *****
  • Posts: 4282
    • Bart en Mariska's Webstek
Re: Writing Record structure to disk and retrieving it
« Reply #3 on: April 30, 2021, 12:47:58 pm »
And it will crash therefore if you try to read it as such...

Maybe better use som XMLConfig of JSONConfig for this?
Even a TIniFile should be possible.

Bart

rwebb616

  • Full Member
  • ***
  • Posts: 133
Re: Writing Record structure to disk and retrieving it
« Reply #4 on: April 30, 2021, 02:05:22 pm »
I have it coded already to write to an INI file however the code is more complex in that I have to writestring for each item in the record so comparatively a lot of lines of code... maybe a hybrid of binary file and ini file and just write the content of the Tstringlist to an ini?

Another reason is I plan to include font information and I thought that the font and it's style could be more easily written if it were stored as a Tfont and Tfontstyle in the record and then written.

Rich

jamie

  • Hero Member
  • *****
  • Posts: 4578
Re: Writing Record structure to disk and retrieving it
« Reply #5 on: April 30, 2021, 11:52:09 pm »
is it suppose to be AssignFile (…..) ?
The only true wisdom is knowing you know nothing

rwebb616

  • Full Member
  • ***
  • Posts: 133
Re: Writing Record structure to disk and retrieving it
« Reply #6 on: May 01, 2021, 12:14:01 am »
Not sure .. I relented and went back to the TINI file .. wrote a function to convert the Tfontstyles into a string to store in the INI and then read it back.  Seems to work.

Rich

jamie

  • Hero Member
  • *****
  • Posts: 4578
Re: Writing Record structure to disk and retrieving it
« Reply #7 on: May 01, 2021, 02:09:53 am »
Ok, well AssignFile be what you use I suppose..
The only true wisdom is knowing you know nothing

egsuh

  • Hero Member
  • *****
  • Posts: 741
Re: Writing Record structure to disk and retrieving it
« Reply #8 on: May 01, 2021, 02:26:33 am »
If you saved something in TStringList then its contents will not be saved to file..probably the address will be stored, I guess.

 

TinyPortal © 2005-2018