2 q997302482 q997302482 于 2016.05.03 09:24 提问

求C语言大牛帮忙解答,输完password之后就异常终止了

/*用户注册文件*/
#include
#include
#define N 1
struct User
{
char num[20];
char name[10];
char password[7];
float balance;
}user[N];
int main()
{
int i;
FILE *fp;
//输入注册信息
for(i = 0;i < N; i++)
{
printf("input num & name & password & balance: \n");
printf("num:");
gets(user[i].num);
printf("name:");
gets(user[i].name);
printf("password:");
gets("user[i].password");
printf("balance:");
scanf("%f",&user[i].balance);
getchar();
}
if((fp = fopen("all user information.dat","ab"))== NULL)
{
printf("It cannot open!");
}
for(i = 0;i < N;i++)
fwrite(&user[0],sizeof(struct User),1,fp);
fclose(fp);
if((fp = fopen("user information.dat","ab"))== NULL)
{
printf("It cannot open!");
}
for(i =0 ;i < N; i++)
fwrite(&user[0],sizeof(struct User),4,fp);
fclose(fp);
printf("注册成功!");

    /*读取用户信息:检查是否成功*/
    if((fp = fopen("user information.dat","r"))== NULL)
    {
        printf("It cannot open!");
        exit(0);
    }
    i = 0;
    while(!feof(fp))
    {
        fread(&user[i],sizeof(struct User),1,fp);
        i++;
    }
    i=i-1;
    fclose(fp);
    printf("\n\n");
    for(i = 0;i < N; i++)
    {
        printf("num:%s\n",user[i].num);
        printf("name:%s\n",user[i].name);
        printf("password:%s\n",user[i].password);
        printf("balance:%.2f\n",user[i].balance);
    }
    return 0;

}

1个回答

caozhy
caozhy   Ds   Rxr 2016.05.03 09:38

gets(user[i].num);
->
gets(&user[i].num);

后面类似

caozhy
caozhy 回复q997302482: 那还有别的错误,仔细调试下
一年多之前 回复
q997302482
q997302482 还是不行。
一年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!