0

I am trying to load a file with data which are extracted in a data structure and edited inside the program but there are some problems. First and foremost I can't extract the data for some reason,no errors, just blank. Second I can't add a new node to the data structure,only one, and then I am stuck in an endless loop because it thinks I want to add something that already exists. Also, when the cases 3 or 2 are selected, I get segmentation fault

I have tried to find the problem with gdb but it didn't give me something useful, tried to switch the orientation of the data inside the file in many way and trigger each case in different order but in the end these problems persist.

#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=malloc(sizeof(struct _node));
    l->size=0;
    return l;
}

void print(node st){
    printf("Name:%s\tId:%.4d\n",st->data.name,st->data.id);
}


void printdata(list l){
int i;
node tmp=l->head;
for(i=0;i<l->size;i++){
    print(tmp);
        tmp=tmp->next;
    }
}


void load(char*filename,list l){
    FILE *fd=fopen(filename,"r");
    node tmp=l->head;
    rewind(fd);
    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 adddata(node st,list l){
    st->next=l->head;
    l->head=st;
    l->size++;
    return 0;
} 


node findid(int id,list l){
        node tmp=l->head;
        while(tmp!=NULL){
                if(tmp->data.id==id){
                        return tmp;
                }
        tmp=tmp->next;
        }
        return NULL;
} 


int deletedata(node st,list l){
    node tmp=l->head;   
    while(tmp->next!=st){
        tmp=tmp->next;
    }
    tmp->next=st->next;
    free(st);
    l->size--;
    return 0;
}



int updatedata(node st,list l){

    printf("Give a name:\n");
    scanf("%s",st->data.name);
    printf("Give id:\n");
    scanf("%d",&st->data.id);

    return 0;
}

int main(int argc,char *argv[]){
    list l=list_create();
    if(argc!=2){
        printf("Input Error\n");
    }
        load(argv[1],l);
        node st=(node)malloc(sizeof(struct _node));
    int action=1;
    node found=(node)malloc(sizeof(struct _node));
    if(st==NULL||found==NULL){
        printf("Error trying to allocate memory");
        return 0;
    }
while(action>=1&&action<=7){
        printMenu();  \\Prints the choices
        scanf("%d",&action);
                switch(action){
                 case 1: \\ add data
                        printf("Give name:\n");
                        scanf("%s",st->data.name);
            printf("Give id\n");
            scanf("%d",&st->data.id);
            found=findid(st->data.id,l); 
            while(found!=NULL){
                printf("This id already exists\n");
                printf("give id\n");
                scanf("%d",&st->data.id);
                found=findid(st->data.id,l);
            }          
                        adddata(st,l);
            break;
        case 2:   \\Delete data
            printf("Give id\n");        
            scanf("%d",&st->data.id);
            found=finddata(st->data.id,l);
            if(found==NULL){
                printf(This id doesn't exist\n");
            }
            else{           
                deletedata(found,l);
            }
            break;
        case 3:     \\Find data
            printf("Give id\n");
            scanf("%d",&st->data.id);
            found=findid(st->data.id,l);
            print(found);


            break;
        case 4:    \\Change name and or id
            printf("Give id\n");
            scanf("%d",&st->data.id);
            found=findid(st->data.id,l);
            updatedata(found,l);
            break;
        case 5:    \\Print one data
            printf("Give id\n");
            scanf("%d",&st->data.id);
            found=findid(st->data.id,l);
            print(found);
            break;
        case 6:   \\Print all data
            printdata(l);
            break;  
        case 7:   \\Terminate 
            action=8;
            break;
        default:
            printf("Please give a valid option\n");
            break;
        }
        }
Azzarian
  • 153
  • 2
  • 13
  • What did you learn when you used the symbolic debugger? – nicomp Jun 13 '19 at 17:51
  • 1
    [Why is “while (!feof(file))” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feoffile-always-wrong) – KamilCuk Jun 13 '19 at 17:52
  • 1
    May want to initialize `next` to `NULL` in `list_create` so you can tell it is not pointing to another node. – Retired Ninja Jun 13 '19 at 17:54
  • Please please please stop typedefing away pointers. It *will* cause you pain and suffering. By all means have a `node*` instead of a `struct node*` but when I see `node` in the code I have very different expectations than `node*` (unless we're talking about opaque structures, but we aren't) – John3136 Jun 14 '19 at 06:48

0 Answers0