我可以还是必须使用自己的SaveToStream方法将其声明为类?

这只是数据,没有函数(尽管我现在可以添加getter和setter)

最佳答案

假设您有以下记录

type
  TMyRecord = record
    FirstName: string[100]; // 100 characters max. for First name
    LastName: string[100]; // 100 characters max. for Last name
    Age: Byte;
    DateOfBirth: TDateTime;
  end;
const
  // if you are using Delphi 2009 and above,
  // then either change *string[100]* to *AnsiString[100]* or use a different
  // approach to save the string, read bellow
  szMyRecord = SizeOf( TMyRecord ); // storing it will make your code run faster if you write a lot of records


现在,为了将上述结构写入流,您需要:

procedure WriteRecord(
  const ARecord: TMyRecord;
  const AStream: TStream // can be a TMemoryStream, TFileStream, etc.
);
begin
  AStream.Write(ARecord, szMyRecord);
end;


重要的是要注意,将FirstName声明为“ string”不会保存FirstName中的字符,您需要像我对“ string [100]”所做的那样声明FirstName或使用特殊方法编写字符串字段,例如:

type
  TMyRecordWithVeryLongStrings = record
    LenFirstName: Integer; // we store only the length of the string in this field
    LenLastName: Integer; // same as above
    Age: Byte;
    DateOfBirth: TDateTime;
    FirstName: string; // we will ignore this field when writing, using it for value
    LastName: string; // same as above
  end;

const
  // we are ignoring the last two fields, since the data stored there is only a pointer,
  // then we can safely assume that ( SizeOf( string ) * 2 ) is the offset
  szMyRecordWithVeryLongStrings = SizeOf( TMyRecordWithVeryLongStrings ) - ( SizeOf( string ) * 2 );

// the difference between this method and above is that we first write the record
// and then the strings
procedure WriteRecord(
  ARecord: TMyRecordWithVeryLongStrings;
  AStream: TStream // can be a TMemoryStream, TFileStream, etc.
);
const szChar = sizeof(char);
begin
  // ensure the length of first and Last name are stored in "Len + Name" field
  ARecord.LenFirstName := Length( ARecord.FirstName );
  ARecoord.LenLastName := Length( ARecord.Lastname );
  // write the record
  AStream.Write(ARecord, szMyRecordWithVeryLongStrings);
  // write First name value
  AStream.Write(
    Pointer( ARecord.FirstName )^, // value of first name
    szChar * ARecord.LenFirstName
  );
  // repeat as above for last name
  AStream.Write(
    Pointer( ARecord.LastName )^, // value of first name
    szChar * ARecord.LenLastName
  );
end;


现在,为了读取“长字符串”,您首先要读取记录:

procedure ReadRecord(
  ARecord: TMyRecordWithVeryLongStrings;
  AStream: TStream
);
begin
  AStream.Read(Arecord, szMyRecordWithVeryLongStrings );
  // now read first and last name values which are right after the record in the stream
  AStream.Read(Pointer(ARecord.FirstName)^, szChar * ARecord.LenFirstName );
  AStream.Read(Pointer(ARecord.,LastrName)^, szChar * ARecord.LenLastName );
end;


希望对您有所帮助(:

关于delphi - 如何将结构写入流?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8045886/

10-10 16:55