有偿!不难 通过文件读写实现用户注册,要识别用户id是否存在
现在只能识别第一行(第一个)用户
```c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct UserData
{
char userID[20];
char userName[20];
char userSex;
char userNumber[20];
char userPass[20];
char userEmail[30];
char userAddress[50];
float usermoney;
}userdata;
void Regist()
{
userdata a={0},b={0};
char temp[20];
char tempid[20];
int q=0;
int t=0;
char tempmoney[50];
char* tmoney;
int n=0;
FILE *pf;
userdata local[20];
pf=fopen("userdata.txt","r");
if(pf==NULL)
{
printf("打开注册文件失败\n");
return;
}
while(!feof(pf))
{
fscanf(pf,"%s %s %s %s %s %s %s %f",local[q].userID,local[q].userName,local[q].userSex,local[q].userNumber,local[q].userPass,local[q].userEmail,local[q].userAddress,&local[q].usermoney);
q++;
}
printf("欢迎注册超市用户账号!( ´∀`)\n");
printf("请输入你的用户ID:\n");
scanf("%s",tempid);
for ( t = 0 ; t < q ; t++)
{
if(strcmp(tempid,local[t].userID)==0)
{
printf("该ID已被用户注册,请输入新的用户ID");
scanf("%s",tempid);
}
else
{
strcpy(a.userID,tempid);
printf("您的ID为:%s",a.userID);
break;
}
}
printf("请输入姓名:\n");
scanf("%s",a.userName);
printf("请输入性别(f(女)/m(男)):\n");
scanf("%c",&a.userSex);
do
{
if(a.userSex != 'f' && a.userSex != 'm')
{
printf("性别输入错误!请重新输入!\n");
scanf("%c",&a.userSex);
}
}while(a.userSex != 'f' && a.userSex != 'm');
getchar();
printf("请输入电话号码:\n");
scanf("%s",a.userNumber);
printf("请输入密码\n");
scanf("%s",a.userPass);
printf("请确认密码\n");
scanf("%s",temp);
do
{
if(strcmp(a.userPass,temp)==0)
{
printf("账号注册成功,请输入邮箱\n");
fclose(pf);
break;
}
else
{
printf("两次密码不匹配!请重新输入!\n");
scanf("%s",a.userPass);
printf("请确认密码\n");
scanf("%s",temp);
}
}while(1);
getchar();
scanf("%s",a.userEmail);
printf("请输入地址\n");
scanf("%s",a.userAddress);
printf("请输入您的余额:");
scanf("%f",&a.usermoney);
pf = fopen("userdata.txt","a");
fwrite(&a, sizeof(struct UserData), 1, pf);
fclose(pf);
return;
}
int main()
{
int input=-1;
do
{
printf("\t\t\t----------------------------------\n");
printf("\t\t\t| 1.登录 |\n");
printf("\t\t\t| 2.注册 |\n");
printf("\t\t\t| 0.退出 |\n");
printf("\t\t\t----------------------------------\n");
printf("请选择您想进行的操作\n");
scanf("%d",&input);
switch(input)
{
case 2:Regist();break;
case 0:puts("退出成功"); return 0;
}
}while(1);
return 0;
}
```