将复杂的记录写入文件 [英] Writing complex records to file
问题描述
Hi
我已经在我的项目中定义了一些可能由其他记录组成的记录,也包括正常数据类型和其他记录的动态数组
它是一个记录类型的例子
Hi I have defined some records in my project which may be consisted of other records and also dynamic arrays of normal data types and other records , it is n example of a record type
Type1=record
x:integer;
end;
Type2=record
Y:array of X;
str:string;
end;
当我尝试使用这样的blockwrite函数将这些记录类型的一个变量保存到文件中时: / p>
When I tried to save one of variables of these records type to file with blockwrite function like this :
var
Temp1:Type2;
begin
setlength(temp1.y,100);
blockwrite(MyFile,Temp1,sizeOf(Temp1);
纯粹的记录的大小是,但temp1有一个dynmic数组,调整大小,
有人可以告诉我如何写一个复杂的记录到一个文件,我的意思是像VB6中使用的东西
谢谢
it just wrote as much as the size of pure record is ,but temp1 has a dynmic arrays which is resized , Could someone please tell me how I can write a complex record to a file , I mean something like what is used in VB6 . Thanks
推荐答案
您可以使用 http://kblib.googlecode.com (与任何动态记录一起使用,还包含其他记录的记录等)。在Delphi 2006/2009 / XE测试(不使用扩展RTTI介绍在D2010中)不需要手动编写保存/加载代码(只需一行来保存/加载任何动态类型 - 字符串,记录,动态数组)。
You can use http://kblib.googlecode.com (works with any dynamic records, also records that contains other records, etc.). Tested on Delphi 2006/2009/XE (it doesn't use extended RTTI introduced in D2010). No need to write save/load code manually (just one line to save/load any dynamic type - strings, records, dynamic arrays).
在你的例如,它将是这样的:
In your example it would be sth. like this:
TKBDynamic.WriteTo(lStream, lType2, TypeInfo(Type2));
要加载它:
TKBDynamic.ReadFrom(lStream, lType2, TypeInfo(Type2));
如果任何人有兴趣如何处理记录版本,只是发布新的问题,然后我'会写一些例子。
If anyone is interested how to deal with 'record versions', just post new issue and then I'll write some examples.
类似的任务:
- How Can I Save a Dynamic Array to a FileStream in Delphi?
- Delphi 2010: How to save a whole record to a file?
这篇关于将复杂的记录写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!