简体   繁体   English

在文件中保存带有指针成员的结构

[英]Save struct with pointer members in file

I'm trying to save a struct into a.dat file and read it back in later.我正在尝试将结构保存到 a.dat 文件中,稍后再读回。

struct myStruct{
  char **one;
  mytype **two;
  mytype2 *three;
}

With an assigning function:使用分配 function:

struct MyStruct get_struct() = {
char **pi = ...;
mytype **pa = ...;
mytype2 **po = ...;
MyStruct n = {pi, pa, po};
return n;
}

I originally tried to save this struct into a.dat file by doing this:我最初试图通过这样做将这个结构保存到 a.dat 文件中:

struct MyStruct s = get_struct();
myoutfile = fopen("file.dat", "w");
if (myoutfile == NULL) {
    fprintf(stderr, "\nError opend file\n");
    exit(1);
}
fwrite(&s, sizeof(struct MyStruct), 1, myoutfile);
fclose(myoutfile);

and read it back in with:并读回:

fread(&t, sizeof(struct MyStruct), 1, myinfile)

Now I learned, that this does not work (segmentation error), because I only save the location where the pointer points to, not the actual thing.现在我了解到,这是行不通的(分段错误),因为我只保存指针指向的位置,而不是实际的东西。

Now my question is, how can I do it properly?现在我的问题是,我怎样才能正确地做到这一点? I have found some solutions for C++ but I need to stay in C.我找到了 C++ 的一些解决方案,但我需要留在 C。

EDIT: Later on, I want to call a function which looks like this:编辑:稍后,我想打电话给 function,它看起来像这样:

void work_with_struct(MyStruct s){
   char ** xone = s.one;
   mytype **xtwo = s.two;
   mytype2 *xthree = s.three;
}

This post is related to this post, but as I could specify my mistake now, asking in a new post makes more sense to me.这篇文章与这篇文章相关,但正如我现在可以指出我的错误一样,在新帖子中提问对我来说更有意义。

As always in programming, you break up the task to smaller chunks, and break up smaller chunks to yet smaller chunks, until every chunk is easy.与编程中的往常一样,您将任务分解为更小的块,然后将更小的块分解为更小的块,直到每个块都很容易。

int saveMyStruct (struct myStruct* myStruct, FILE* file) {
   // what do I do here?!?!
   // well it has three members
   // so treat each one in sequence
   int result;
   result = saveStringArray(myStruct->one, file);
   if (result >= 0)
     result = saveMyTypeArray (myStruct->two, file);
   if (result >= 0)
     result = saveMyType (myStruct->three, file);
   return result;
}

Note how the status is checked all the time.请注意如何始终检查状态。 If you work with files, you need to check the status all the time.如果您使用文件,则需要始终检查状态。

What next?接下来是什么? You need to write three functions mentioned above.您需要编写上面提到的三个函数。

 saveStringArray(char** stringArray, FILE* file)
 {
     // first save the length of the array, then save each individual string
     int length = getStringArrayLength(stringArray);
     int result = fwrite(&length, sizeof(length), 1, file);
     if (result != 1)
        return -1;
     for (i = 0; i < length; ++i)
     {
        result = saveString(stringArray[i], file);
        if (result < 0)
           return -1;
     }
     return i;            
 }

And so on and so forth.等等等等。 I presume your array of pointers is NULL-terminated;我假设你的指针数组是以 NULL 结尾的; if not, you need to have some other way to know its length.如果没有,您需要通过其他方式知道它的长度。

Note how array length is always saved before array elements.请注意数组长度如何始终在数组元素之前保存。 This is because you will need to read your array later, and you will need to know where to stop.这是因为您稍后需要读取数组,并且需要知道在何处停止。 It will also be easy to allocate your array when you read it.阅读时分配数组也很容易。

I'm trying to save a struct into a.dat file and read it back in later.我正在尝试将结构保存到 a.dat 文件中并稍后再读回。

struct myStruct{
  char **one;
  mytype **two;
  mytype2 *three;
}

With an assigning function:使用分配 function:

struct MyStruct get_struct() = {
char **pi = ...;
mytype **pa = ...;
mytype2 **po = ...;
MyStruct n = {pi, pa, po};
return n;
}

I originally tried to save this struct into a.dat file by doing this:我最初尝试通过执行以下操作将此结构保存到 a.dat 文件中:

struct MyStruct s = get_struct();
myoutfile = fopen("file.dat", "w");
if (myoutfile == NULL) {
    fprintf(stderr, "\nError opend file\n");
    exit(1);
}
fwrite(&s, sizeof(struct MyStruct), 1, myoutfile);
fclose(myoutfile);

and read it back in with:并将其读回:

fread(&t, sizeof(struct MyStruct), 1, myinfile)

Now I learned, that this does not work (segmentation error), because I only save the location where the pointer points to, not the actual thing.现在我了解到,这不起作用(分段错误),因为我只保存指针指向的位置,而不是实际的东西。

Now my question is, how can I do it properly?现在我的问题是,我怎样才能正确地做到这一点? I have found some solutions for C++ but I need to stay in C.我找到了一些 C++ 的解决方案,但我需要留在 C。

EDIT: Later on, I want to call a function which looks like this:编辑:稍后,我想调用一个看起来像这样的 function:

void work_with_struct(MyStruct s){
   char ** xone = s.one;
   mytype **xtwo = s.two;
   mytype2 *xthree = s.three;
}

This post is related to this post, but as I could specify my mistake now, asking in a new post makes more sense to me.这篇文章与这篇文章有关,但正如我现在可以指出我的错误一样,在新帖子中提问对我来说更有意义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM