简体   繁体   中英

Access violation error on fread

This is the partial code:

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();
}

I'm getting an error on the fread part, fwrite is fine. When I try to check if the 2nd or 3rd account could be retrieved the whole program just stops, 1st account seems fine, 2nd and 3rd not.

EDIT: Thanks for those who answered, I finally found the source of my problem, You've all been very helpful. Thanks for the advice. :)

You fread into p but never allocate memory for it, either change p to

nd p;

and pass &p as the first argument to fread , or malloc memory for it:

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

EDIT : I see that you assign p = *head; in logon and pass head in from main , so the code in logon should work but head isn't allocated in main . So change nd * head to nd head or malloc it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM