0

I am trying to create a function which loads data from a .txt file but when it runs I always get a segmentation fault(core dumped) error. The file contains an unknown number of lines while each line has a string and an integer separated by tab.The list_create function just creates a data structure. The while loop in the end deletes the data structure, I did not include the code because I am sure it does not cause the problem but I also want show that I am freeing the data structure.It is worth mentioning that when is gdb used, I get:

Program received signal SIGSEGV, Segmentation fault.
0x0000555555554c46 in load (filename=0x7fffffffe2ab "students.txt", 
    l=0x555555757260) at Student.c:92
92                  tmp->next=malloc(sizeof(struct _node));

I have tried to change the feof with something else,use with and without ferror and change the mode for fopen to r instead of a.

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.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;
};


list list_create(){
    list l=(list) malloc(sizeof(struct _list));
    assert(1);
    l->head=NULL;
    l->size=0;
    return l;
}
void load(char*filename,list l){
    FILE *fd=fopen(filename,"r");
    node tmp=l->head;

    if(fd==NULL){
        printf("Error trying to open the file\n");
                abort();
    }
    else{

                while(!feof(fd)&&!ferror(fd)){

        fscanf(fd,"%s\t%d\n",tmp->data.name,&tmp->data.id);   
                tmp->next=(node)malloc(sizeof(struct _node));
        assert(tmp->next);
        tmp=tmp->next;                
        l->size++;
            if (tmp==NULL){
                printf("Error trying to allocate memory\n");
                abort();
            }
                }      
        }

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



int main(int argc,char *argv[]){ 
list l=list_create();
if(argc!=2){
        printf("Input Error\n");
    }
    load(argv[1],l);
\*Some code*\


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

    free(l);
        return 0;

I am expecting to load the file successfully, be able to edit its components and save them.

Azzarian
  • 153
  • 2
  • 13
  • 1
    Build your program with debug-symbols and run it under a debugger. It will tell you fairly quickly where the most immediate issue is. Unless you're `list_create` manages to automagically hang enough nodes to contain your entire file (which would be odd) the root issue is you never allocate nodes for your scanf to populate. Now, about that debugger.... – WhozCraig Jun 10 '19 at 20:01
  • 1
    Please provide a correct (readable) indentation of the code. – user803422 Jun 10 '19 at 20:09
  • It would be nice to see the code for `list_create`. It's hard to say what is wrong when we don't have everything. Some other tips: Do not hide pointers behind `typedefs`. In your case you don't have to use `malloc` (or `calloc`) to allocate `struct _list` on the heap. `C` (and `C++`) is *NOT* `java`, you don't have to allocate all the `struct` (or `class`) on the heap. All your `struct _node` on the other hand, must (since you don't know how many you need) be allocated with `malloc` (or similar). Typically that is done while filling up the list with data (in your case while reading) – HAL9000 Jun 10 '19 at 20:25
  • 2
    Possible duplicate of [segmentation fault (core dump) while loading a file](https://stackoverflow.com/questions/56563850/segmentation-fault-core-dump-while-loading-a-file) – Rup Jun 12 '19 at 14:02
  • You already have asked that same question and there is [an answer with 6 upvotes](https://stackoverflow.com/a/56563958/898348). – Jabberwocky Jun 12 '19 at 15:01

1 Answers1

1

Your segmentation fault may be explained by the fact that your function list_create() allocates one data structure, but your function load() populates as many data structures as lines in the file, thus writing data in unallocated space.

user803422
  • 2,636
  • 2
  • 18
  • 36