使用文件中的指针成员保存结构 [英] Save struct with pointer members in file

查看:38
本文介绍了使用文件中的指针成员保存结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个结构体保存到一个 .dat 文件中,稍后再读回来.

I'm trying to save a struct into a .dat file and read it back in later.

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

带有赋值函数:

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

我最初尝试通过这样做将此结构保存到 .dat 文件中:

I originally tried to save this struct into a .dat file by doing this:

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

然后再读一遍:

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.

现在我的问题是,我怎样才能正确地做到这一点?我已经找到了一些 C++ 的解决方案,但我需要继续使用 C.

Now my question is, how can I do it properly? I have found some solutions for C++ but I need to stay in C.

稍后,我想调用一个如下所示的函数:

Later on, I want to call a function which looks like this:

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

等等等等.我认为您的指针数组是以 NULL 结尾的;如果没有,您需要通过其他方式知道其长度.

And so on and so forth. I presume your array of pointers is NULL-terminated; 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.

这篇关于使用文件中的指针成员保存结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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