一、实现登录功能。
自定义一个usr.txt,手动输入账户密码,格式如下:账户 密码
例如: zhangsan 12345
lisi abcde
wangwu abc123
需求如下:
- 从终端获取账户密码,与文件中的账户密码比较
- 若终端输入的账户不存在,则输出账户不存在
- 若终端输入的账户存在,但是密码不正确,则输出密码错误
- 若账户密码均正确,则输出登录成功
/*自定义usr.txt*/
chenqian 123456
wangfeng 123456
#include <stdio.h>
#include <string.h>
int main(int argc, const char *argv[])
{
char name[10];
char password[10];
printf("name:");
scanf("%s",name);
printf("password:");
scanf("%s",password);
FILE *fp = fopen("./usr.txt","r");
if(NULL == fp)
{
perror("fopen");
return -1;
}
char name_t[10];
char password_t[10];
int c;
while(strcmp(name,name_t) != 0)
{
c = fscanf(fp,"%s%s",name_t,password_t);
if(EOF == c)
break;
}
if(0 == strcmp(name,name_t))
{
if(0 == strcmp(password,password_t))
printf("成功登陆!\n");
else
printf("密码错误!\n");
}
else
printf("账户不存在!\n");
if(fclose(fp) < 0)
{
perror("fclose");
return -1;
}
return 0;
}
/*输入输出样例*/
ubuntu@ubuntu:day1$ gcc homework.c
ubuntu@ubuntu:day1$ ./a.out
name:wangfeng
password:123456
成功登陆!
ubuntu@ubuntu:day1$ ./a.out
name:wangfeng
password:123
密码错误!
ubuntu@ubuntu:day1$ ./a.out
name:zhangsan
password:123456
账户不存在!
附加题:
实现注册功能,注册的账户密码存储在上一题的文件中。不能重复注册
#include <stdio.h>
int main(int argc, const char *argv[])
{
char c;
printf("Do you want to register:");
scanf("%c",&c);
if('Y' == c || 'y' == c)
{
char name[10];
char password[10];
printf("name:");
scanf("%s",name);
char name_t[10];
int a;
FILE *fp = fopen("usr.txt","r");
while(0 != strcmp(name,name_t))
{
a = fscanf(fp,"%s",name_t);
if(EOF == a)
break;
}
if(0 == strcmp(name,name_t))
{
printf("用户名已存在!\n");
return -1;
}
else
{
printf("password:");
scanf("%s",password);
fclose(fp);
fp = fopen("usr.txt","a");
fprintf(fp,"%s %s\n",name,password);
}
fclose(fp);
}
return 0;
}
总代码:
#include <stdio.h>
#include <string.h>
int main(int argc, const char *argv[])
{
char name[10]="";
char password[10]="";
char name_t[10]="";
char password_t[10]="";
int c;
printf("name:"); //输入用户名
scanf("%s",name);
FILE *fp = fopen("./usr.txt","r"); //打开文件
while(0 != strcmp(name,name_t))
{
c = fscanf(fp,"%s%s",name_t,password_t);
if(EOF == c)
break;
}
if(0 != strcmp(name,name_t))
{
printf("账户不存在!\n");
fclose(fp);
char a;
printf("Do you want to register:");
scanf(" %c",&a);
if('Y' == a || 'y' == a)
{
printf("name:");
scanf("%s",name);
fp = fopen("usr.txt","r");
while(0 != strcmp(name,name_t))
{
c = fscanf(fp,"%s",name_t);
if(EOF == c)
break;
}
if(0 == strcmp(name,name_t))
{
printf("用户名已存在!\n");
return -1;
}
else
{
printf("password:");
scanf("%s",password);
fclose(fp);
fp = fopen("usr.txt","a");
fprintf(fp,"%s %s\n",name,password);
}
}
}
else
{
printf("password:");
scanf("%s",password);
if(0 == strcmp(password,password_t))
printf("成功登陆!\n");
else
printf("密码错误!\n");
}
return 0;
}
二、用fgetc与fputc函数实现:
- 文件拷贝,例如将1.txt的内容拷贝到2.txt中
- 要求用fgetc计算一个文件有多少个字节
- 用fgetc计算一个文件有几行?
#include <stdio.h>
int main(int argc, const char *argv[])
{
FILE *fp = fopen("./1.txt","r");
FILE *fp_t = fopen("./2.txt","w");
if(NULL == fp)
{
perror("fopen");
return -1;
}
char c;
int count=0,line=0;
while((c = fgetc(fp)) != EOF)
{
count++;
if(c == '\n')
line++;
fputc(c,fp_t);
}
printf("文件有%d字节,有%d行\n",count,line);
fclose(fp);
fclose(fp_t);
return 0;
}