尝试打开文件时出现分段错误(核心已转储) [英] Segmentation fault (core dumped) while trying to open a file

查看:64
本文介绍了尝试打开文件时出现分段错误(核心已转储)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个程序,该程序使用姓名和ID创建人员的数据结构.当我编译时没问题,但是运行时我遇到了段错误(核心转储).该文件与.c文件位于同一文件夹中.此外,在文件内部,数据将由制表符分隔.list_create()函数以列表形式创建数据结构.

I am trying to create a program which creates a data structure of people with their names and ids. When I it compiles no problem but when it runs I get segmentation fault(core dumped). The file is on the same folder as the .c file.Also, inside the file the data will be separated by tab. The list_create() function creates the data structure in the form of a list.

我尝试不编写一行包含很多功能的代码,也不编写一行包含很少功能的代码,而是将tmp用作列表而不是节点的列表,以不同的顺序释放变量,但是它什么都没有改变.

I tried instead of making one single line of code with many functions, multiple lines with few functions,use the tmp as a list instead of a node,free variables in a different order but it changed nothing.

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#define MAXSTRING 50
typedef struct{
    char name[MAXSTRING];
    int id;
} student;
typedef struct _node* node;
typedef struct _list* list;

struct _node {
    student data;
    node next;
};

struct _list {
    node head;
    int size;
};


int list_empty(list l){
        assert(l);
        return l->head==NULL;
}
node list_deletefirst(list l){
        assert(l && l->head);
        node ret;
        l->head=l->head->next;
        l->size--;
        ret->next=NULL;
        return ret;
}

void list_freenode(node n){
        assert(n);
        free(n);
}


void load(char*filename,list l){
    FILE *fd;
    node tmp=l->head;
    if((fd=fopen(filename,"r"))==NULL){
        printf("Error trying to open the file");
                abort();
    }
    else{
                while(!feof(fd)){
                fscanf(fd,"%s\t%d\n",tmp->data.name,&tmp->data.id);

                tmp=tmp->next;
                l->size++;
                }

        }

    tmp->next=NULL;
    fclose(fd);
}

void save(char *filename,list l){
    int i;
    node tmp=l->head;
    FILE *fd;   
    rewind(fd);
    if((fd=fopen(filename,"w"))==NULL){
        printf("File could not be opened");
        abort();
    }
    for(i=0;i<l->size;i++){
        fprintf(fd,"%s\t%.4d\n",tmp->data.name,tmp->data.id);
        tmp=tmp->next;
    }
    rewind(fd);
    fclose(fd);
}

int main(int argc,char *argv[]){
    list l=list_create();
    load(argv[1],l);


    save(argv[1],l);

    while (!list_empty(l)){
                list_freenode(list_deletefirst(l));
        }
    free(l);

    return 0;
}

我希望得到一个包含名称和ID的列表.

I expect to get a list with names and ids.

推荐答案

free(l->head->next);
l->head=l->head->next;

您要释放 l-> head-> next ,并在下一行中将释放的值(垃圾)分配给 l-> head .

You are freeing l->head->next and assigning the freed value (garbage) to l->head in the next line.

因此,当您尝试访问 l-> head-> next 时,您将在第二次迭代中读取垃圾(segfault).

In consequence you read garbage (segfault) in the second iteration when trying to access to l->head->next.

这篇关于尝试打开文件时出现分段错误(核心已转储)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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