Saturday, November 25, 2017

DELPHI - How to generate file with record based type

When you need save some complex record type to file you can use this code:

{ header of admin file }
TRecFileAdmin = record
  sIdent              : string[15] ;
  dDateTo             : TDateTime;
end;
...
{ ---------------------------------------------------------------------------
  Function generate admin file into _FileName file.
  -------------------------------------------------------------------------- }
function GenerateAdminFile( _sFileName : string ) : boolean;
var
  f : TFile;
  recFileAdmin : TRecFileAdmin;
begin
  result := false;

  try
    try
      { every time establish new }
      f := TFile.Create( _sFileName, true );

      { three day validity }
      FillChar( recFileAdmin, sizeof( recFileAdmin ), 0 );
      recFileAdmin.sIdent  := gcsIdent;
      recFileAdmin.dDateTo := Now + 3;  
      f.FileWrite( recFileAdmin, sizeof( recFileAdmin ) );
    except
      on E : exception do
        begin
          doError( ERROR_SAVE, [ 'A9Admin.GenerateAdmin()', E.Message ] );
          exit;
        end;
    end;
  finally
    f.free;
  end;

  result := true;
end;

No comments:

Post a Comment