Link to home
Start Free TrialLog in
Avatar of PeterDelphin
PeterDelphin

asked on

Write/Read Record to/from IniFile with WriteBinaryStream/ReadBinaryStream?

In Delphi (XE7) I need to store a record in an IniFile with WriteBinaryStream:

Pseudocode:

IniFile.WriteBinaryStream('ASection', 'AName', GetStream(MyRecord));

Open in new window


Then read the single record fields with a simple method like:

Pseudocode:

MyRecord := GetRecordFromStream(Inifile.ReadBinaryStream(...));
Value1 := MyRecord.ValueX;
Value2 := MyRecord.Valuey;

Open in new window


How can all this be done?
SOLUTION
Avatar of jimyX
jimyX

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of PeterDelphin
PeterDelphin

ASKER

With your methods I can store and read only strings. To store and read a stringlist to/from an inifile I wouldn't need WriteBinaryStream/ReadBinaryStream. That's why I have to use a record: Using different data types.
I understand. That was just an example of passing TStringList to Stream to ini.
But you could easily rework that with the code form SO link provided to add records.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This is the best solution for the initial question: It is easy to implement and allows to save records to an inifile.
Accept. This system is very confusing. I don't know whether I should Object or Object. What does this mean?
Even you close, ... I can suggest writing Base64 encoded binary record. It is quite easy to do this.
@Sinisa Vuk AFAIS, doesn't uKBDynamic just do this?
@Sinisa Vuk How does this relate to my question " store a record in an IniFile with WriteBinaryStream"? The question is not how to convert a stream to a string representation but how to convert a record to a stream.
this is my example:

uses ..., DCPbase64;

Type
  PMyRec = ^TMyRec;
  TMyRec = record
    ValueX: integer;
    ValueY: Integer;
  end;

procedure TForm1.Button5Click(Sender: TObject);
var
  ini: TIniFile;
  myrec: TMyRec;
  sRec: AnsiString;
begin
  myrec.ValueX := 200;
  myrec.ValueY := 100;

  ini := TIniFile.Create(ChangeFileExt(Application.ExeName, '.ini'));
  try
    //make string long enough
    SetLength(sRec,((SizeOf(myrec)+2) div 3) * 4);
    //encode
    Base64Encode(@myrec, PAnsiChar(sRec), SizeOf(myrec));
    //write to ini
    ini.WriteString('Data', 'MyRec', sRec);
  finally
    ini.Free;
  end;
end;

procedure TForm1.Button6Click(Sender: TObject);
var
  ini: TIniFile;
  myrec: TMyRec;
  sRec: AnsiString;
begin
  ini := TIniFile.Create(ChangeFileExt(Application.ExeName, '.ini'));
  try
    //read from ini
    sRec := ini.ReadString('Data', 'MyRec', '');
    if Length(sRec) > 0 then
    begin
      //decode
      sRec := Base64DecodeStr(sRec);
      //set record
      if Length(sRec) = SizeOf(myrec) then
        CopyMemory(@myrec, PAnsiChar(sRec), SizeOf(myrec));
    end;
  finally
    ini.Free;
  end;
end;

Open in new window


...and for this example I use unit for base 64 encoding: DCPbase64.pas
Alternatively, you can use some other.
This is the best solution for the initial question: It is easy to implement and allows to save records to an inifile.