输入信息为什么写不到文件里面?怎么把信息保存到文件中?

img

img

img

1-查询文件之后增加close操作
2-fopen写操作时候"w"改成"a+",不然会覆盖之前的内容,造成信息丢失

修改完善如下,供参考:

int regist()
{
    land a,b;
    FILE *fp;
    char temp[20];
    int  count = 0;
    printf("\t\t\t-------------------欢迎来到注册界面---------------------\n");
    printf("\t\t\t请输入账号:");
    getchar();
    scanf("%s",a.Id);
    fp = fopen("登录.txt","r");
    if(fp != NULL){
        fread(&b,sizeof(land),1,fp);
        while(1){
             if(strcmp(a.Id,b.Id)!=0){
                if(!feof(fp)){
                       fread(&b,sizeof(land),1,fp);
                }else  break;
             }else{
                printf("\t\t\t此用户名已存在!请重新注册!\n");
                fclose(fp);
                return menu();
             }
        }
        fclose(fp);
    }
    printf("\t\t\t请输入密码:");
    getchar();
    scanf("%s",a.pwd);
    printf("\t\t\t请再次输入密码:");
    getchar();
    scanf("%s",temp);
    do{
        if(strcmp(a.pwd,temp)==0){
            fp = fopen("登录.txt","a+");
            fwrite(&a,sizeof(land),1,fp);
            printf("\t\t\t账号注册成功,请登录!\n");
            fclose(fp);
            return menu();
        }else{
            printf("\t\t\t两次密码不匹配!请重新输入!\n");
            getchar();
            scanf("%s",a.pwd);
            printf("\t\t\t请确认密码\n");
            getchar();
            scanf("%s",temp);
        }
    }while(1);
}