回答
假设你有以下记录现在
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声明为“st环”将不保存在名字中的字符,你需要声明名字和我一样‘的字符串[100]’或使用特殊的方法写一个字符串字段,例如:现在
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;
我希望它有助于(:
+1,并感谢这样一个全面的例子!我将复制/粘贴并测试它,并在我得到它的时候给予答案。谢谢 !! – Mawg
请注意,这只适用于短字符串(字符串[xx])。他们只是Ansi。所有其他的字符串都是指针,你需要更多的工作来写它们。 –
ShortStrings自从2009年德尔福以来真的被弃用了。这是恕我直言,不是记录保存的好选择,而且会浪费太多空间。 –
http://code.google.com/p/kblib/ - 你可以试试这个(没有必要限制字符串常量的大小和复杂的工作记录,加载/保存一行)。
类似的问题:的[编写复杂的记录到文件]
- 1. 将结构写入NSMutableArray
- 2. 如何将流写入内存流?
- 3. C++阅读结构的领域,直接写入结构到流
- 4. 写入结构
- 5. 如何将值传递给结构变量,然后将结构写入文件?
- 6. 结构化流式传输将实验性文件写入hadoop
- 7. 如何在Go中写入DIBits结构?
- 8. 如何从GUI写入结构
- 9. FFMPEG/libav:UYVY422如何写入AVFrame结构?
- 10. 如何将argotic.feed.save结果写入内存流?
- 11. Elixir:试图将地图写入CSV,正在写入流结果
- 12. 如何将流数据写入S3?
- 13. 如何将对象写入流?
- 14. 如何使用XmlWriter将System.Xml.Linq.XElement写入流
- 15. 如何将流写入文件?
- 16. 如何将流数据集写入Cassandra?
- 17. 如何将NAudio WaveStream写入内存流?
- 18. 将StringBuilder写入流
- 19. 将结构和类写入磁盘
- 20. 将Jenkins构建结果写入JSON
- 21. 将结构写入Swift中的outputStream 3
- 22. 将结构写入文件C
- 23. 将结构化数据写入printf
- 24. 无法将数据写入结构
- 25. 将结构体写入文件
- 26. 将链接结构写入类
- 27. 将文件写入结构数组
- 28. 如何将结构的成员写入文件?
- 29. 如何有效地将大数据结构写入文件?
- 30. 如何使用fwrite将结构写入文件?
可能重复(http://stackoverflow.com/questions/4533376/写复杂的记录到文件) –