用C语言在数据库文件中保存结构
我想将一个结构保存到一个db文件中(或者.txt,这仍然不重要!),但是我遇到了以下问题。我希望像下面的代码一样在结构内部创建结构用C语言在数据库文件中保存结构,c,database,struct,tree,save,C,Database,Struct,Tree,Save,我想将一个结构保存到一个db文件中(或者.txt,这仍然不重要!),但是我遇到了以下问题。我希望像下面的代码一样在结构内部创建结构 typedef struct classes cl; typedef struct attribute a; struct classes{ \\where "a" is a type of struct a hunter; a channeler; a warrior; a rogue; }; struct human{
typedef struct classes cl;
typedef struct attribute a;
struct classes{ \\where "a" is a type of struct
a hunter;
a channeler;
a warrior;
a rogue; };
struct human{ \\where "cl" is type of struct classes (
cl Borderlands;
cl Shienear;
cl Arafel;
cl Illian;
cl Tear;
cl Tarabon;
cl Andor;
cl TwoRivers;
cl Amandor;
cl Mayene;
cl Murandy;
};
问题是我是否有一个变量
结构化人工数据
我必须保存树的所有分支(因为我认为它是我创建的树),还是只保存根,保存整个结构
另外,请原谅我的写作方式,我在编程方面没有那么丰富的经验您应该为每一种结构创建保存方法,如下所示:
void save_h(human * h, FILE * stream)
{
save_cl(h->Borderlands,stream);
save_cl(h->Shienear,stream);
save_cl(h->Arafel,stream);
save_cl(h->Illian,stream);
save_cl(h->Tear,stream);
save_cl(h->Tarabon,stream);
...
}
void save_cl(classes * cl, FILE * stream)
{
save_a(cl->hunter,stream);
save_a(cl->channeler,stream);
save_a(cl->warrior,stream);
save_a(cl->rogueon,stream);
...
}
void save_a(attribute * a, FILE * stream)
{
...
}
我会这样做:
#define STRUCTFLAG 565719 // some random number
// NOTE: This is based on the idea that sizeof(int) == sizeof(int *).
// If this is wrong, then make the type of variables such that
// sizeof(typeof variable) = sizeof(int *).
struct basestruct {
int flag; // when initialized, this has the value of STRUCTFLAG, so that we know it's a struct
int size; // total size of the struct, in bytes. set when struct is created.
// all other variables in the struct are either pointers to other structs or of the primitive type of the size 'int *'.
}
struct mystruct {
int flag;
int size;
struct mystruct2 *variable1;
struct mystruct3 *variable2;
}
int isStruct(const void *structAddr)
{
int *casted = (int *) structAddr;
return casted[0] == STRUCTFLAG;
}
void saveStruct(FILE *file, const void *structaddr)
{
// make sure it's a struct
if (isStruct(structaddr))
{
int *casted = (int *) structaddr;
fprintf(file, "%i\n", casted[0]); // print flag
casted++; // skip 'flag';
fprintf(file, "%i\n", casted[0]); // print size
int numVariables = ((casted[0] / sizeof(int)) - 2);
casted++;
for (int i = 0; i < numVariables; i++)
{
if (isStruct(casted + i))
{
saveStruct(file, casted + i);
}
else
{
fprintf(file, "%i\n", casted[i]);
}
}
}
else
{
// report error
}
}
#定义STRUCTFLAG 565719//一些随机数
//注意:这是基于sizeof(int)==sizeof(int*)的思想。
//如果这是错误的,那么将变量的类型设置为
//sizeof(typeof变量)=sizeof(int*)。
结构basestruct{
int flag;//初始化时,它的值为STRUCTFLAG,因此我们知道它是一个struct
int size;//结构的总大小,以字节为单位。在创建结构时设置。
//结构中的所有其他变量要么是指向其他结构的指针,要么是大小为“int*”的基元类型。
}
结构mystruct{
int标志;
整数大小;
结构mystruct2*变量1;
结构mystruct3*变量2;
}
int isStruct(常量void*structAddr)
{
int*casted=(int*)structAddr;
返回casted[0]==STRUCTFLAG;
}
void saveStruct(文件*FILE,常量void*structaddr)
{
//确保它是一个结构
if(isStruct(structaddr))
{
int*casted=(int*)structaddr;
fprintf(文件“%i\n”,铸造[0]);//打印标志
casted++;//跳过“标志”;
fprintf(文件“%i\n”,铸造[0]);//打印大小
int numVariables=((casted[0]/sizeof(int))-2);
铸造++;
对于(int i=0;i
不过,祝你在回读时好运。你只是问如何保存它 如果您有没有指针的简单结构、固定大小的字段类型,并且不打算将此数据移动到其他机器上,那么您只需将整个结构写入二进制文件,因为它在内存中具有线性表示。然后以同样的方式读回来。否则,请阅读有关封送和解封送数据的内容。如果你不理解这个概念,没有一个代码是非常有用的。这个问题是用C++来标记的,而不是C++。函数重载在C中不可用。还要注意,这不是很灵活。将另一个结构类型添加到列表中会很痛苦。当然,它是不可扩展的。有没有可能以这种方式保存这棵树,使它的生长就像存储中的“附录”?
sizeof(int)==sizeof(int*)
是一个可怕的假设。即使您将int
更改为intptr\u t
,您也无法在计算机之间传输保存的文件。我也不喜欢未声明的要求,即任何变量都不会碰巧具有值STRUCTFLAG
——这将导致随机查找并可能难以再现故障。@interjay true,但这是我的第一个想法。虽然不可移植,但它是一个简单的实现,适用于较小的数据结构。我并没有说这个方法没有它的警告和可能的错误,但它是一个灵活的向下和肮脏的解决方案。