C语言,文件输入输出问题

#include<stdio.h>#include<stdlib.h>#include<string.h>#definemax20structperson{charnam... #include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define max 20

struct person
{
char name[max];
char password[max];
char city[max];
}yonghu;

int LogIn()
{
int i=0;
char name[max],password[max];
FILE *fp;
if((fp=fopen("person.dat","rb"))==NULL)
{
printf("\ncan not open file");
exit(0);
}
fread(&yonghu,sizeof(struct person),1,fp);
printf("\nplease put your name: ");
scanf("%s",name);
printf("\nplease put your password: ");
while((i>=0)&&((password[i++]=getch())!=13))
{
if(password[i-1]=='\b')
{
printf("%c%c%c",'\b','\0','\b');
i=i-2;
}
else
printf("*");
}
password[--i]='\0';
if(!strcmp(name,yonghu.name)&&!strcmp(password,yonghu.password))
{
printf("\nsuccess");
return 1;
}
else
{
printf("\nerror");
return 0;
}
}

void Register()
{
char *p="person.dat";
FILE *fp;
printf("\nplease enter your usename: ");
scanf("%s\n",yonghu.name);
printf("please enter your password: ");
scanf("%s\n",yonghu.password);
printf("please enter your city: ");
scanf("%s\n",yonghu.city);
if((fp=fopen("person.dat","wb"))==NULL)
{
printf("\nOpen file %s error!",p);
system("pause");
exit(0);
}

if(fwrite(&yonghu,sizeof(struct person),1,fp)!=1)
printf("\nfile write error");
fclose(fp);
}

void Go()
{
printf("1.Log In\n");
printf("2.Register\n");
printf("3.Log Out\n");
printf("please choose: ");
switch(getch())
{
case '1':LogIn();break;
case '2':Register();Go();break;
case '3':exit(0);break;
default: printf("\nerror!\n");Go();break;
}
}

void main()
{
Go();
}
为什么选择2后usename输入完后还要再输入个东西?
展开
 我来答
宿啥品8397
推荐于2017-09-21 · TA获得超过4529个赞
知道大有可为答主
回答量:3273
采纳率:60%
帮助的人:1794万
展开全部
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define max 20

  struct person
  {
char name[max];
char password[max];
char city[max];
  }yonghu;

  int LogIn()
  {
int i=0;
char name[max],password[max];
FILE *fp;
if((fp=fopen("person.dat","rb"))==NULL)
{
printf("\ncan not open file");
exit(0);
}
fread(&yonghu,sizeof(struct person),1,fp);
printf("\nplease put your name: ");
scanf("%s",name);
printf("\nplease put your password: ");
while((i>=0)&&((password[i++]=getch())!=13))
{
if(password[i-1]=='\b')
{
printf("%c%c%c",'\b','\0','\b');
i=i-2;
}
else
printf("*");
}
password[--i]='\0';
if(!strcmp(name,yonghu.name)&&!strcmp(password,yonghu.password))
{
printf("\nsuccess");
return 1;
}
else
{
printf("\nerror");
return 0;
}
  }

  void Register()
  {
char *p="person.dat";
FILE *fp;
printf("\nplease enter your usename: ");
scanf("%s\n",yonghu.name);
printf("please enter your password: ");
scanf("%s\n",yonghu.password);
printf("please enter your city: ");
scanf("%s\n",yonghu.city);
if((fp=fopen("person.dat","wb"))==NULL)
{
printf("\nOpen file %s error!",p);
system("pause");
exit(0);
}

if(fwrite(&yonghu,sizeof(struct person),1,fp)!=1)
printf("\nfile write error");
fclose(fp);
  }

  void Go()
  {
printf("1.Log In\n");
printf("2.Register\n");
printf("3.Log Out\n");
printf("please choose: ");
switch(getch())
{
case '1':LogIn();break;
case '2':Register();Go();break;
case '3':exit(0);break;
default: printf("\nerror!\n");Go();break;
}
}

  void main()
  {
Go();
  }
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式