卡在一个怪异的循环中,没有文件

时间:2019-06-13 17:47:34

标签: c loops

我正在尝试使用在数据结构中提取并在程序内部进行编辑的数据加载文件,但是存在一些问题。首先,由于某种原因,我无法提取数据,没有错误,只是空白。其次,我不能将新节点添加到数据结构中,只能添加一个,然后陷入无限循环,因为它认为我想添加已经存在的节点。此外,当选择案例3或2时,我会遇到细分错误

我试图找到gdb的问题,但是它没有给我有用的东西,试图以多种方式切换文件内数据的方向并以不同的顺序触发每种情况,但最终这些问题仍然存在

#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;
        }
        }

0 个答案:

没有答案