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输入完后还要再输入个东西? 展开
#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输入完后还要再输入个东西? 展开
1个回答
展开全部
#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();
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |