标准IO函数练习

一、实现登录功能。

自定义一个usr.txt,手动输入账户密码,格式如下:账户 密码

例如: zhangsan 12345

            lisi abcde

            wangwu abc123

需求如下:

  1. 从终端获取账户密码,与文件中的账户密码比较
  2. 若终端输入的账户不存在,则输出账户不存在
  3. 若终端输入的账户存在,但是密码不正确,则输出密码错误
  4. 若账户密码均正确,则输出登录成功
/*自定义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. 文件拷贝,例如将1.txt的内容拷贝到2.txt中
  2. 要求用fgetc计算一个文件有多少个字节
  3. 用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;
}

  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值