fread上的访问冲突错误

时间:2013-10-10 16:00:12

标签: c

这是部分代码:

typedef struct {
    int dd, mm, yy;
} dateType;

typedef struct {
    char username[50], password[50];
    int acctype;
    dateType date;
} usertype;

typedef struct {
    float transactions;
    char use[50];
    dateType date;
} transactype;

typedef struct node {
    usertype employee[2];
    usertype admin;
    transactype trans[3];
    float funds;
    int department;
    struct node *next;
} nd;

void logon(nd **);
void admin_reg(nd **);

void main(void)
{
    int choice;
    nd *head, *p;

    do {
        printf("MENU:\n");
        printf("1.Log In\n");
        printf("2.Admin/Manager Registration\n");
        printf("3.Exit\n");
        printf("Enter your choice:");
        scanf("%d",&choice);

        switch(choice)
        {
            case 1:
                logon(&head);
                clrscr();
            break;
            case 2:
                admin_reg(&head);
                clrscr();
            break;
            case 3:
                exit(1);
            break;
            default:
                printf("Invalid choice.\n");
            break;
        }
    }while(choice!=3);
}

void admin_reg(nd **head)
{
    int i;
    nd *p;
    FILE *fp;

    if((fp=fopen("Admin.txt","w"))==NULL)
    {
        printf("file not found");
        exit(1);
    }

    *head = (nd*)malloc(sizeof(nd));
    printf("admin username: ");
    scanf("%s",(*head)->admin.username);
    printf("admin password: ");
    scanf("%s",(*head)->admin.password);
    printf("Admin department:1 2 3: ");
    scanf("%d",&(*head)->department);
    (*head)->admin.acctype=3;
    fwrite(*head, sizeof(nd), 1, fp);

    p = *head;

    for(i = 2; i <= 3; i++)
    {
        p->next = (nd*)malloc(sizeof(nd));
        printf("admin username: ");
        scanf("%s",p->next->admin.username);
        printf("admin password: ");
        scanf("%s",p->next->admin.password);
        printf("Admin department:1 2 3: ");
        scanf("%d",&p->next->department);
        p->next->admin.acctype=3;
        fwrite(p->next,sizeof(nd),1,fp);
        p = p->next;
    }
    p->next = NULL;

    fclose(fp);
}

void logon(nd **head)
{
    nd *p;
    char username[50], password[50];
    p=*head;
    FILE *fp;

    if((fp=fopen("Admin.txt","r"))==NULL)
    {
        printf("file not found");
        exit(1);
    }

    printf("Input username:");
    scanf("%s",username);
    printf("Input password:");
    scanf("%s",password);

    while(fread(p, sizeof(nd), 1, fp)==1)
    {  
        if(strcmp(p->admin.username,username)==0 && strcmp(p->admin.password,password)==0)
        {
            puts(p->admin.username);
            puts(p->admin.password);
            printf("\nSuccessfully compared!");getch();
        }
        /* else
        {
            for(x=0;x<2;x++)
                if(strcmp(username,p->employee[x].username)==0 && strcmp(password,p->employee[x].password)==0)
                {
                    y++;
                }
        } */

        p=p->next;
    }

    fclose(fp);getch();
}

我在fread部分出错,fwrite很好。当我尝试检查第二个或第三个帐户是否可以检索时,整个程序就停止了,第一个帐户似乎没问题,第二个帐户和第三个帐户没有。

编辑:感谢那些回答,我终于找到了问题的根源,你们都非常乐于助人。感谢您的建议。 :)

1 个答案:

答案 0 :(得分:1)

fread进入p但从未为其分配内存,请将p更改为

nd p;

并将&p作为freadmalloc内存的第一个参数传递给它:

nd * p = malloc(sizeof(nd));

编辑:我看到您在p = *head;中分配logon并从head传递main,因此{{1}中的代码应该可以工作,但logon中没有分配head。因此,将main更改为nd * headnd head