如何将结构写入流? [英] How to write a structure to a stream?
问题描述
我可以还是必须使用它自己的SaveToStream方法将其声明为一个类?
Can I, or do I have to declare it as a class with it's own SaveToStream method?
这只是数据,没有函数(尽管我现在可能会添加getters和setters)
It is only data, no functions (although I might now add getters & setters)
推荐答案
假设您有以下记录
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
现在,按顺序执行将使您的代码运行更快要将以上结构写入流,您需要:
Now, in order to write the above structure to a stream, you need to:
procedure WriteRecord(
const ARecord: TMyRecord;
const AStream: TStream // can be a TMemoryStream, TFileStream, etc.
);
begin
AStream.Write(ARecord, szMyRecord);
end;
请务必注意,将FirstName声明为字符串不会将字符保存在FirstName中,需要像我 string [100]一样声明FirstName或使用特殊方法编写字符串字段,例如:
it is important to note that declaring FirstName as "string" will not save the characters in FirstName, you need to declare FirstName as I did "string[100]" or use a special method to write a string field, for example:
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;
现在,为了读取长字符串,您首先要读取记录:
Now, in order to read the "long strings", you first read the record:
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;
我希望它会有所帮助(:
I hope it helps (:
这篇关于如何将结构写入流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!