读取文件时无法找出总线错误:10

时间:2014-11-19 07:50:52

标签: c function pointers linked-list bus-error

我正在创建一个程序来读取文本文件并将其放入链表中,然后继续执行其他功能。该程序还涉及一个每5秒运行一次的线程函数,但崩溃发生在启动时。这是导致问题的函数的代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>

//Strucutres
#define NODE struct node
struct node {
    int number;
    char name[20];
    NODE *next;
};

#define LIST struct list
struct list {
    NODE *head;
    NODE *tail;
};

#define PARAM struct param
struct param {
    char filename[20];
    LIST *listpar;

};

pthread_mutex_t mutex;

//Declare functions
void read_file(LIST **, char *in);
LIST * add(LIST **list, char *fake, int fakenumber);
void show(LIST **list);
void delete(LIST **list);
void dispbin(char *input, LIST **list);
void *autosaver(void *arg);

int main(int argc, char *argv[]) {
    LIST *list;
    FILE *in;
    PARAM *newP;
    printf("%s",argv[1]);
    printf("%s",argv[2]);

    pthread_t thr;
    pthread_mutex_init(&mutex, NULL);

    if ((list=(LIST *)malloc(sizeof(LIST)))==NULL) {
        printf("No memory available");
        return 2;
    }
    list->head=NULL;
    list->tail=NULL;
    if (argc==1) {
        printf("No files entered");
        return 1;
    } else if (argc==2) {
        printf("Text file entered but no binary file entered");
        return 1;
    }
    else {
        printf("Reading file");
        read_file(&list, argv[1]);

    }
    strcpy(newP->filename,argv[2]);
    newP->listpar=list;

    printf("About to create");
    pthread_create(&thr, NULL, autosaver,(void *)newP);
    int whileLoop=0;
    int tempnumber;
    char tempName[20];
    while (whileLoop==0) {
        newP->listpar=list;
        int selection;
        printf("\nPick an option:1)Add New Party 2)Enter Open Table Size 3)Show Text List 4)Show Binary Autosave 5)Quit: ");
        scanf("%d",&selection);
        NODE *new=list->head;
        switch(selection) {
            case 1:
                printf("\nWhat is the name of the party? ");
                scanf("%s",tempName);
                printf("\nWhat is the size of the party? ");
                scanf("%d",&tempnumber);
                pthread_mutex_lock(&mutex);
                add(&list,tempName,tempnumber);
                pthread_mutex_unlock(&mutex);
                break;
            case 2:
                pthread_mutex_lock(&mutex);
                delete(&list);
                pthread_mutex_unlock(&mutex);
                break;
            case 3:
                pthread_mutex_lock(&mutex);
                show(&list);
                pthread_mutex_unlock(&mutex);
                break;
            case 4:
                pthread_mutex_lock(&mutex);
                dispbin(argv[argc-1],&list);
                pthread_mutex_unlock(&mutex);
                break;
            case 5:
                pthread_cancel(thr);

                if ((in=fopen(argv[argc-2],"w"))==NULL) {
                    printf("File not found");
                    return 3;

                }
                fprintf(in, "Name Group Size\n-------------\n");
                while (new!=NULL) {
                    //printf("Hi2");
                    fprintf(in,"%s %d\n",new->name,new->number);
                    new=new->next;
                }
                fclose(in);
                whileLoop=1;
                break;
            default:
                printf("\nWhat is the name of the party? ");
                scanf("%s",tempName);
                printf("\nWhat is the size of the party? ");
                scanf("%d",&tempnumber);
                pthread_mutex_lock(&mutex);
                add(&list,tempName,tempnumber);
                pthread_mutex_unlock(&mutex);
                break;




        }
    }


}

void read_file(LIST **list, char *in) {
    printf("%s",in);
    printf("Start of read function");
    printf("\n");
    int seats;
    char name[20];
    FILE *infp;
    if ((infp=fopen(in,"r"))==NULL) {
        printf("File not found\n");
        return;
    }
    printf("Opened");
    fseek(infp, sizeof(char)*strlen("Name Group Size\n-------------"),SEEK_SET);
    printf("Seeked");
    while (fscanf(infp,"%s %d",name,&seats)!=EOF) {
        add(list, name,seats);
    }
    fclose(infp);
    printf("End of read function");
    return;
}
LIST * add(LIST **list, char *fake, int fakenumber) {
    printf("\n");
    NODE *new;
    NODE *temp;
    if ((new=(NODE *)malloc(sizeof(NODE)))==NULL) {
        printf("No memory!");
        return *list;

    }
    if ((*list)->head==NULL) {
        strcpy(new->name,fake);
        new->number=fakenumber;
        (*list)->head=new;
        (*list)->tail=new;
        new->next=NULL;
        return *list;
    }
    temp=(*list)->head;
    while (temp!=NULL) {
        if (strcmp(fake,temp->name)==0) {
            printf("Name already taken");
            return *list;

        }
        temp=temp->next;

    }
    if ((new=(NODE *)malloc(sizeof(NODE)))==NULL) {
        printf("No memory");
        return *list;
    }
    strcpy(new->name,fake);
    new->number=fakenumber;
    (*list)->tail->next=new;
    (*list)->tail=new;
    new->next=NULL;

    return *list;

}

0 个答案:

没有答案