0

这是部分代码:

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 很好。当我尝试检查是否可以检索第二个或第三个帐户时,整个程序就停止了,第一个帐户似乎很好,第二个和第三个没有。

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

4

1 回答 1

1

fread进入p但从不为其分配内存,要么更改p

nd p;

&p作为第一个参数传递给它fread,或者它的malloc内存:

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

编辑:我看到您分配p = *head;inlogon并传入headfrom main,因此 in 中的代码logon应该可以工作,head但未分配 in main。所以改成nd * headornd headmalloc

于 2013-10-10T16:18:59.843 回答