Einzelnen Beitrag anzeigen

Benutzerbild von sniper_w
sniper_w

Registriert seit: 12. Dez 2004
Ort: Wien, Österriech
893 Beiträge
 
Delphi 6 Enterprise
 
#6

Re: record in datei speichern und laden

  Alt 29. Jan 2006, 19:00
Ausschnitt aus diesem Projekt:
Delphi-Quellcode:
type

  PConnection = ^TConnection;
  TConnection = packed record
    Description: string;
    Host: string;
    Port: Integer;
    User: string;
    Password: string;
    TimeOut: Integer;
    UseCompression : Boolean;
    UseTimeOut : Boolean;
    // later used...
    DB:string;
    Table:string;
  end;
....
procedure TConnectionList.LoadConnections;
  var MyFile: TFileStream;
      i, len, len1: Integer;
      currentConnection: PConnection;
begin
  if FileExists( ConnFile ) then
  begin

    MyFile := TFileStream.Create( ConnFile, fmOpenRead );
    try
      MyFile.Read( len, SizeOf(len) );
      //SetLength( ConnectionList, len );

      for i:=0 to len-1 do
      begin

        New( currentConnection );

        with currentConnection^ do
        begin
          // read description
          MyFile.Read( len1, SizeOf(len1) );
          SetLength( Description, len1 );
          MyFile.Read( PChar(Description)^, len1 );
          // read host
          MyFile.Read( len1, SizeOf(len1) );
          SetLength( Host, len1 );
          MyFile.Read( PChar(Host)^, len1 );
          // read user
          MyFile.Read( len1, SizeOf(len1) );
          SetLength( User, len1 );
          MyFile.Read( PChar(User)^, len1 );
          // read password
          MyFile.Read( len1, SizeOf(len1) );
          SetLength( Password, len1 );
          MyFile.Read( PChar(Password)^, len1 );
          // read port
          MyFile.Read( Port, SizeOf(Port) );
          // read timeout
          MyFile.Read( TimeOut, SizeOf(TimeOut) );
          // read UseCompression
          MyFile.Read( UseCompression, SizeOf( UseCompression ) );
          // read UseTimeOut
          MyFile.Read( UseTimeOut, SizeOf( UseTimeOut ) );
        end;

        Add( currentConnection );

      end;

    finally
      MyFile.Free();
    end;

  end else
  begin
    // no saved connections at this time...
  end;
end;
...
procedure TConnectionList.SaveConnections;
  var i, len, len1:integer;
      stream : TFileStream;
begin
  stream := TFileStream.Create( ConnFile, fmCreate );
  try
    // save the number of connections to remember...
    len := Count;
    stream.Write( len, SizeOf(len) );

    for i:=0 to len-1 do
    begin

      with TConnection(Items[i]^) do
      begin
        // save description
        len1 := Length( Description );
        stream.Write( len1, SizeOf( len1) );
        stream.Write( PChar(Description)^, len1 );
        // save host
        len1 := Length( Host );
        stream.Write( len1, SizeOf( len1) );
        stream.Write( PChar(Host)^, len1 );
        // save user
        len1 := Length( User );
        stream.Write( len1, SizeOf( len1) );
        stream.Write( PChar(User)^, len1 );
        // save password
        len1 := Length( Password );
        stream.Write( len1, SizeOf( len1) );
        stream.Write( PChar(Password)^, len1 );
        // save port
        stream.Write( Port, SizeOf( Port ) );
        // save TimeOut
        stream.Write( TimeOut, SizeOf( TimeOut ) );
        // save UseCompression
        stream.Write( UseCompression, SizeOf( UseCompression ) );
        // save UseTimeOut
        stream.Write( UseTimeOut, SizeOf( UseTimeOut ) );
      end;

    end;

  finally
    stream.Free();
  end;
end;
Katura Haris
Es (ein gutes Wort) ist wie ein guter Baum, dessen Wurzel fest ist und dessen Zweige in den Himmel reichen.
  Mit Zitat antworten Zitat