C数据序列化? [英] C Data Serialize ?

查看:97
本文介绍了C数据序列化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我需要将一个具有指针成员的(复杂)结构保存到文件中,然后再加载它。必须保存指针指向

的实际内存,而不是内存地址。


我是否需要创建de / serialization函数?如果是这样,什么是好的

起点?


是否可以检查变量是否为结构,指针等。?


谢谢,

Corne''

!排除免责声明!

解决方案

2003年7月31日星期四17:05:46 +0200,Corne''Cornelius写道:



我需要将一个具有指针成员的(复杂)结构保存到文件中,然后再加载它。


我知道这样做的唯一方法就是自己编写代码,是一个

de / serialize函数。

如果是这样,什么是好的
起点?

我认为这取决于应用程序。

中是否包含二进制数据?结构中有多少成员。什么样的存储对您的应用程序有效?等等。
我(或我们任何人)帮助你的最好方法是看一些示例代码。


我没有必要保存/加载许多复杂的结构,当我这样做时,如果配置文件(最常用的ini文件格式)并且只有

函数来加载值,我会将数据布局

阅读回到结构中。

是否可以检查变量是否为结构,指针等。?



总有一些技巧你可以这样做,但它是所有

架构/平台特定的。在我看来最好避免。


-

Sig

Thu Jul 31 11:11:42 EDT 2003





好​​吧,能够加载/保存任何结构都很好,所以

序列化可能就是答案。


例如:


#include< stdio.h>

#include< string.h>


int main(int argc,char * argv []){

FILE * f;

struct _x {

int id;

char * name;

} x = {

187,

NULL

};


x.name = strdup(" corne");


f = fopen(" test.out"," wb");

fwrite(& x,1,sizeof(struct _x),f) ;

fclose(f);


免费(x.name);


bzero(& x ,sizeof(struct _x));


f = fopen(" test.out"," rb");

fread(& x, 1,sizeof(struct _x),f);

fclos e(f);


返回0;

}

通过调试编译,看看''x ''在写之前,

和读后。你会看到,如果x.name被写入outfile文件中的内存地址,而不是该内存地址的值。这是

正确,我知道。但我需要能够恢复一个完整的结构,因为它是b $ b。


谢谢,

Corne''

signuts写道:

2003年7月31日星期四17:05:46 + 0200,Corne''Cornelius写道:



我需要将一个具有指针成员的(复杂)结构保存到文件中,然后再加载它。



我知道这样做的唯一方法就是自己编写代码,是一个/序列化函数。

如果是这样的话什么是好的
起点?



我认为这取决于应用程序。
您的结构中是否包含二进制数据?结构中有多少成员。什么样的存储对您的应用程序有效?
我(或我们任何人)帮助你的最好方法是看一些示例代码。

我不必保存/加载许多复杂的结构如果配置文件(最常用的是ini文件格式)并且只是具有将读取的值加载到结构中的
函数,我会布置数据。

< blockquote class =post_quotes>是否有可能检查变量是否是结构,指针等。?



总有一些技巧你可以做,但它是'所有
架构/平台特定。在我看来最好避免。




On Thu,2003年7月31日11:05:46 -0400,Corne''Cornelius写道:



我需要将一个具有指针成员的(复杂)结构保存到文件中,然后再加载它。必须保存指针指向
的实际内存,而不是内存地址。

我是否需要创建de / serialization函数?如果是这样,那么起点是什么?


这可能是一个很好的起点:

http://www.ioplex.com/~miallen/encdec/


但你需要自己编写更高版本level de / serialization

用于对指针指向的数据进行de / serialize。请参阅

tests / t3encdec.c。

是否可以检查变量是否为结构,指针等。?




不足以自动化序列化过程。你必须明确地这样做




Mike


Hi,

I need to save a (complex) struct which has pointer members, to a file,
and load it again later. The actual memory which the pointers point to
must be saved, not the memory address.

Will i need to create de/serialization functions ? if so, what''s a good
starting point ?

is it possible to check if a variable is a struct, pointer, etc.. ?

Thanks,
Corne''
!Exclude Disclaimer!

解决方案

On Thu, 31 Jul 2003 17:05:46 +0200, Corne'' Cornelius wrote:

Hi,

I need to save a (complex) struct which has pointer members, to a file,
and load it again later.
The only way I''m aware of doing this is to write teh code yourself, yes a
de/serialize functions.
if so, what''s a good
starting point ?
I think this depends on the application. Is there binary data contained in
your structures? How many members are in the structure. What kind of
storage would be effecient for your application? etc.
The best way for Me (or any of us) to help you is to see some example code.

I havn''t had to save/load many complex structures and when I do I lay out
the data if a config file (ini file format most often) and just have
functions to load the values read right back into the structure.
is it possible to check if a variable is a struct, pointer, etc.. ?


There is always some trickery you can do but it''s all
architecture/platform specific. Best avoided in my opinion.

--
Sig
Thu Jul 31 11:11:42 EDT 2003


Hi,

Well, it would be nice to be able to load/save any struct, so
serialization is probably the answer.

eg.:

#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[]) {
FILE *f;
struct _x {
int id;
char *name;
} x = {
187,
NULL
};

x.name = strdup("corne");

f = fopen("test.out", "wb");
fwrite(&x, 1, sizeof(struct _x), f);
fclose(f);

free(x.name);

bzero(&x, sizeof(struct _x));

f = fopen("test.out", "rb");
fread(&x, 1, sizeof(struct _x), f);
fclose(f);

return 0;
}
compile that with debugging, and have a look at ''x'' before the write,
and after the read. you''ll see, the memory address if x.name was written
to the outfile, instead of the value at that memory address. This is
correct, i know. but i need to be able to restore a complete struct as
it was.

Thanks,
Corne''
signuts wrote:

On Thu, 31 Jul 2003 17:05:46 +0200, Corne'' Cornelius wrote:

Hi,

I need to save a (complex) struct which has pointer members, to a file,
and load it again later.


The only way I''m aware of doing this is to write teh code yourself, yes a
de/serialize functions.

if so, what''s a good
starting point ?



I think this depends on the application. Is there binary data contained in
your structures? How many members are in the structure. What kind of
storage would be effecient for your application? etc.
The best way for Me (or any of us) to help you is to see some example code.

I havn''t had to save/load many complex structures and when I do I lay out
the data if a config file (ini file format most often) and just have
functions to load the values read right back into the structure.

is it possible to check if a variable is a struct, pointer, etc.. ?



There is always some trickery you can do but it''s all
architecture/platform specific. Best avoided in my opinion.




On Thu, 31 Jul 2003 11:05:46 -0400, Corne'' Cornelius wrote:

Hi,

I need to save a (complex) struct which has pointer members, to a file,
and load it again later. The actual memory which the pointers point to
must be saved, not the memory address.

Will i need to create de/serialization functions ? if so, what''s a good
starting point ?
This might be a good starting point:

http://www.ioplex.com/~miallen/encdec/

but you will need to write your own higher level de/serialization
functions to de/serialize the data pointed to by the pointers. See
tests/t3encdec.c.
is it possible to check if a variable is a struct, pointer, etc.. ?



Not enough to automate the serialization process. You must do it
explicitly.

Mike


这篇关于C数据序列化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆