Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C分段错误-在.dat文件中保存结构_C_Pointers_Fault - Fatal编程技术网

C分段错误-在.dat文件中保存结构

C分段错误-在.dat文件中保存结构,c,pointers,fault,C,Pointers,Fault,有人能告诉我,是什么原因导致我的C代码出现分段错误?我正在尝试将结构保存到文件中,然后按照指南调用它。我没有看到任何错误的分配,所以我很高兴知道我的错误是由更有经验的人 这是我的代码,简化版: int main(int argc, char **argv) { char *key = (argc > 4) ? argv[4]: 0; if(0==strcmp(key, "write")){ struct MyStruct s;

有人能告诉我,是什么原因导致我的C代码出现分段错误?我正在尝试将结构保存到文件中,然后按照指南调用它。我没有看到任何错误的分配,所以我很高兴知道我的错误是由更有经验的人

这是我的代码,简化版:

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);
}
另外,正如我在中读到的,这样做:

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)工作正常。我认为没有必要首先提及的是,在“else”的末尾,我有一个与
t
一起工作的函数。我相信这是一个抛出错误的人。 当我省略.dat文件部分时,它工作得很好,所以

t = get_struct(argv[2]);
work_with_struct(t);
实际上我想避免,因为“get_struct”是巨大的。我的解决方案是只计算一次,然后处理.dat文件中的数据。 我现在的假设是,将结构放入fstream并将其取回将以某种方式破坏它,或者使它以某种方式不可读,以便
使用\u struct
。我认为还值得一提的是,在结构中有三个成员:两个char**,一个自定义数据类型。 我没有找到任何其他的解决方案来建议文件中的其他读取方式


也许有人会因为这个解释而得到警告,分段错误可能来自哪里。非常感谢

问题在于,您正在向strcmp传递一个空指针,它需要一个const char*。当没有参数传递给程序key=0时,由于strcmp试图取消引用此空指针,程序SEGMP将出现故障

编辑: 我试图更正示例程序,使其编译并运行,如下所示:

编辑2:打印已保存结构的内容

#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);
    }
}
#包括
#包括
#包括
结构MyStruct{
INTA;
};
int main(int argc,字符**argv){
常量字符*键=(argc>4)?argv[4]:“写入”;
如果(0==strcmp(键,“写入”)){
结构MyStruct s;
文件*myoutfile;
myoutfile=fopen(“file.dat”,“w”);
if(myoutfile==NULL){
fprintf(stderr,“\n打开文件时出错\n”);
出口(1);
}
s=(struct MyStruct){a'};
fwrite&s,sizeof(struct MyStruct),1,myoutfile;
fclose(myoutfile);
}否则{
结构MyStruct;
文件*myinfile;
myinfle=fopen(“file.dat”,“r”);
if(myinfle==NULL){
fprintf(stderr,“\n打开文件时出错\n”);
出口(1);
}
while(fread&t,sizeof(struct MyStruct),1,myinfle))
printf(“完成读取\n”);
printf(“%c\n”,t.a.);
fclose(myinfle);
}
}
编辑3:
更新struct MyStruct的描述后,问题显而易见。您需要找到一种不同的方法将结构保存到文件中,然后使用fwrite,因为其中有指针类型。这是另一个问题的主题。

问题是,您正在向strcmp传递一个空指针,它需要一个const char*。当没有参数传递给程序key=0时,由于strcmp试图取消引用此空指针,程序SEGMP将出现故障

编辑: 我试图更正示例程序,使其编译并运行,如下所示:

编辑2:打印已保存结构的内容

#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);
    }
}
#包括
#包括
#包括
结构MyStruct{
INTA;
};
int main(int argc,字符**argv){
常量字符*键=(argc>4)?argv[4]:“写入”;
如果(0==strcmp(键,“写入”)){
结构MyStruct s;
文件*myoutfile;
myoutfile=fopen(“file.dat”,“w”);
if(myoutfile==NULL){
fprintf(stderr,“\n打开文件时出错\n”);
出口(1);
}
s=(struct MyStruct){a'};
fwrite&s,sizeof(struct MyStruct),1,myoutfile;
fclose(myoutfile);
}否则{
结构MyStruct;
文件*myinfile;
myinfle=fopen(“file.dat”,“r”);
if(myinfle==NULL){
fprintf(stderr,“\n打开文件时出错\n”);
出口(1);
}
while(fread&t,sizeof(struct MyStruct),1,myinfle))
printf(“完成读取\n”);
printf(“%c\n”,t.a.);
fclose(myinfle);
}
}
编辑3:
更新struct MyStruct的描述后,问题显而易见。您需要找到一种不同的方法将结构保存到文件中,然后使用fwrite,因为其中有指针类型。这是一个不同问题的主题。

事实上,示例代码甚至没有编译。您确定此代码是产生segfault的代码吗?示例中是否缺少代码?@L.Grozinger我的原始代码是编译的,也许我在“简化本文的代码”时在某个地方输入了错别字。当然,现在没有提到像get_struct这样的函数。在我试图保存结构之前,我的代码工作正常,这就是为什么我只提取有问题的部分。在我的例子中,它将被编译,我只在运行它时得到一个错误。如果能够在调试器下编译并运行代码,以识别SEGFULT发生的位置,这将非常有用。你试过了吗?简单地阅读一堆代码并尝试猜测segfault可能发生在何处并不容易……示例代码无法编译的原因有很多。指南也有点不确定,因为他们写“if(fwrite!=0)”的部分总是正确的。你看到了吗,编译器哪里有问题?恐怕我不能给函数get_struct,因为它同样依赖于其他函数,但我可以保证,它可以工作,它只返回某种结构。我希望有个打字员