2012-03-12 20 views
1

構造体をdbファイル(またはまだ問題のない.txt)に保存したいのですが、次の問題があります。私は構造体の内部に次のコードのような構造体を作成したかったのです。Cのデータベースファイルに構造体を保存する

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; 
}; 

私は変数 構造体の人間のデータを持っている場合、質問は 私は(私はそれが私が作成した木であることを考えると)、ツリーのすべてのブランチを保存する必要があり、あるいは単に保存することによりんさルート、構造体全体を保存しますか?

P.S.足すの私の方法を言い訳、私はあなたがこのようなすべての構造のための方法保存するべきプログラミング

答えて

2

で経験しているないですしてください:ここでは

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) 
{ 
    ... 
} 
+0

この質問はCではなくCでタグ付けされています。関数のオーバーロードはC言語では利用できません。 –

+0

申し訳ありませんが、 – k06a

+0

の名前を変更します。これはあまり柔軟性がないことに注意してください。リストに別の構造体の型を追加するのは苦労でしょう。 –

0

は、私はそれを行うだろうかです:

#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 
    } 
} 

それを読んでも良い幸運。あなたはそれを保存する方法を尋ねただけです!

+0

sizeof(int)== sizeof(int *)はひどい仮定です。また、 'int'を' intptr_t'に変更しても、保存されたファイルをマシン間で転送することはできません。 – interjay

+0

私はまた、変数が "STRUCTFLAG"という値を持つことはまったくないという無言の要求が好きではありません - それはランダムな見た目を引き起こし、失敗を再現するのが難しいかもしれません。 – interjay

+0

@interjay true、これは私が最初に考えたものです。ポータブルではありませんが、簡単な実装であり、小規模のデータ構造でも使用できます。 –

0

固定サイズのフィールド型のポインタを持たないシンプルな構造体を持ち、このデータを他のマシンに移動する予定がない場合は、メモリー内に線形表現があるため、構造全体をバイナリファイルに書き込むことができます。同じようにそれを読んでください。それ以外の場合は、データの整列化と非整列化についてをお読みください。このコンセプトを理解していないと、コードは実際には非常に役に立ちません。

関連する問題