C 分段错误 - 在 .dat 文件中保存结构 [英] C Segmentation fault - saving struct in .dat file

查看:71
本文介绍了C 分段错误 - 在 .dat 文件中保存结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我,是什么导致了我的 C 代码中的分段错误?我正在尝试将结构保存到文件中,然后按照 this 指南.我没有看到任何错误分配,所以我很高兴向更有经验的人了解我的错误.

Can someone please tell me, what causes the segementation fault in my C - code? I am trying to save a struct into a file, and calling it afterwards, following this guide. I don't see anything assigned wrongly, so I'm happy to learn what's my mistake by someone more experienced.

这是我的简化代码:

int main(int argc, char **argv)
{
    char *key = (argc > 4) ? argv[4]: 0;
    if(0==strcmp(key, "write")){
        struct MyStruct s;
        FILE *myoutfile;
        myoutfile = fopen("file.dat","w")
        if (myoutfile == NULL) 
        { 
                    fprintf(stderr, "\nError opend file\n"); 
                    exit (1); 
        } 
        s = get_struct(argv[2]);
        fwrite(&s, sizeof(struct MyStruct), 1, myoutfile);
        fclose(myoutfile);
    }else{
    struct MyStruct t;
    FILE *myinfile;
    myinfile = fopen("file.dat", "r")
    if (myinfile == NULL) 
    { 
                fprintf(stderr, "\nError opend file\n"); 
                exit (1); 
    } 
    while (fread(&t, sizeof(struct MyStruct), 1, myinfile))
        printf("Done reading");
    }
    work_with_struct(t);
    fclose(myinfile);
}

此外,正如我在另一篇堆栈溢出帖子中所读到的那样:

Also, as I read in another stack overflow post, doing this:

fread(&t.one, sizeof(t.one), 1, myinfile);
fread(&t.two, sizeof(t.two), 1, myinfile);
fread(&t.three, sizeof(t.three), 1, myinfile);
            

也没有用.

我现在想我已经找到了更多的问题.函数 (if) 的第一部分工作正常.我认为没有必要首先提到的是,在其他"的结尾处.我有一个与 t 一起工作的函数.我相信这是抛出错误的原因.当我省略 .dat-file-part 时,它工作正常,所以只需说

I now think i have located the problem a bit more. The first part of the function (if) works fine. What I thought was not necessary to mention first, was that in the end of the "else" I have a function that works with t. This is the one throwing the error, i believe. It works fine, when I leave out the .dat-file-part, so by just saying

t = get_struct(argv[2]);
work_with_struct(t);

我实际上想避免,因为get_struct"很大.做一次,并使用 .dat 文件中的数据是我的解决方案,我只计算一次.我现在的假设是,将结构体放入 fstream 并取回它会以某种方式破坏它,或者使其对于 work_with_struct 不可读.我觉得还值得一提的是,struct里面有三个成员:两个char **,一个自定义数据类型.我没有找到任何其他解决方案建议以其他方式阅读文件.

which I actually want to avoid, because "get_struct" is huge. Doing it once, and working with the data in the .dat file was my solution, that I calculate it only once. My assumption now is, that putting the struct into the fstream and getting it back will somehow destroy it, or makes it somehow not-readable for work_with_struct. What I think is also worth mentioning is, that inside the struct there are three members: two char **, and one custom data type. I didn't find any other solution suggesting other ways of reading in the file.

也许通过这种解释,有人会收到警报,说明分段错误可能来自哪里.非常感谢!

Maybe with this explanation someone gets alerted, where the segmentation fault might come from. Thanks a lot!

推荐答案

问题是您将 NULL 指针传递给需要 const char* 的 strcmp.当没有参数传递给程序 key = 0 并且程序段错误时,因为 strcmp 试图取消引用这个 NULL 指针.

The issue is that you are passing a NULL pointer to strcmp which expects a const char*. When no arguments are passed to the program key = 0 and the program segfaults because strcmp attempts to dereference this NULL pointer.

我试图更正示例程序以使其编译和运行,这里是:

I attempted to correct the example program to make it compile and run, here it is:

Edit 2: 打印保存结构体的内容

Edit 2: print content of saved struct

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
struct MyStruct {
    int a;
};
int main(int argc, char **argv) {
    const char *key = (argc > 4) ? argv[4] : "write";
    if (0 == strcmp(key, "write")) {
            struct MyStruct s;
            FILE *myoutfile;
            myoutfile = fopen("file.dat", "w");
            if (myoutfile == NULL) {
                    fprintf(stderr, "\nError opend file\n");
                    exit(1);
            }
            s = (struct MyStruct){ 'a' };
            fwrite(&s, sizeof(struct MyStruct), 1, myoutfile);
            fclose(myoutfile);
    } else {
            struct MyStruct t;
            FILE *myinfile;
            myinfile = fopen("file.dat", "r");
            if (myinfile == NULL) {
                    fprintf(stderr, "\nError opend file\n");
                    exit(1);
            }
            while (fread(&t, sizeof(struct MyStruct), 1, myinfile))
                    printf("Done reading\n");
            printf("%c\n", t.a);
            fclose(myinfile);
    }
}

编辑 3:更新结构 MyStruct 的描述后,问题就很明显了.您需要找到一种不同的方法来将结构保存到文件中,然后使用 fwrite,因为其中包含指针类型.你会怎么做是另一个问题的主题.

Edit 3: After updating the description of the struct MyStruct the problem is obvious. You need to find a different way to save the struct to the file then by using fwrite since you have pointer types in it. The way you would go about that is a topic for a different question.

这篇关于C 分段错误 - 在 .dat 文件中保存结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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