7.25 IO fprintf fscanf fputc fgetc

7.25 IO fprintf fscanf fputc fgetc_第1张图片

 在usr.txt中输入账号密码

#include
#include
#include
int main(int argc, const char *argv[])
{
	FILE *fp=fopen("usr.txt","w");
	if(NULL==fp){
		perror("fopen");
		return -1;
	}
	while(1){
		printf("输入账号:");
		char str[20]="";
		scanf("%s",str);
		fprintf(fp,"%s ",str);

		printf("输入密码:");
		char str1[20]="";
		scanf("%s",str1);
		fprintf(fp,"%s\n",str1);

		printf("是否继续输入y/n\n");
		char a;
		scanf("%*c%c",&a);
		if(a=='n')
			break;
	}

	if(fclose(fp)<0){
		perror("fclose");
		return -1;
	}
	return 0;
}

 7.25 IO fprintf fscanf fputc fgetc_第2张图片

登录


#include
#include
#include
int main(int argc, const char *argv[])
{
	FILE *fp=fopen("usr.txt","r");
	if(NULL==fp){
		perror("fopen");
		return -1;
	}

	char str[20]="";
	char str1[20]="";

	char arr[20]="";
	char arr1[20]="";

	printf("输入账号:");
	scanf("%s",str);
	printf("输入密码:");
	scanf("%s",str1);
	while(1){
		fscanf(fp,"%s",arr);
		int a=fscanf(fp,"%s",arr1);
		if(EOF==a)
			break;
		if(strcmp(arr,str)==0){
			if(strcmp(arr1,str1)==0){
				printf("登入成功\n");
				return -1;
			}
			else{
				printf("密码错误\n");
				return -1;
			}
		}
	}
	printf("账号不存在\n");
	return 0;
}

7.25 IO fprintf fscanf fputc fgetc_第3张图片

 注册

#include
#include
#include
int main(int argc, const char *argv[])
{
	FILE *fp=fopen("usr.txt","a+");
	if(NULL==fp){
		perror("fopen");
		return -1;
	}

	printf("输入要注册的账号:");
	char str[20]="";
	scanf("%s",str);

	printf("输入注册的账号的密码:");
	char str1[20]="";
	scanf("%s",str1);

	while(1){
		char arr[20]="";
		char arr1[20]="";

		fscanf(fp,"%s",arr);
		int a=fscanf(fp,"%s",arr1);
		if(EOF==a)
			break;
		if(strcmp(arr,str)==0){
			printf("用户已存在\n");
			return -1;
		}
	}

	fprintf(fp,"%s ",str);
	fprintf(fp,"%s\n",str1);
	printf("注册成功\n");
	return 0;
}

7.25 IO fprintf fscanf fputc fgetc_第4张图片

 文件中输入单字符

#include
#include
#include
int main(int argc, const char *argv[])
{
	FILE *fp=fopen("1.txt","w");
	if(NULL==fp){
		perror("fopen");
		return -1;
	}

	fputc('a',fp);
	fputc('b',fp);
	fputc('c',fp);

	if(fclose(fp)<0){
		perror("fclose");
		return -1;
	}
	return 0;
}

拷贝到2.txt,计算字节,计算行数

#include
#include
#include
int main(int argc, const char *argv[])
{
	FILE *fp=fopen("1.txt","r");
	if(NULL==fp){
		printf("__%d__\n",__LINE__);
		perror("fopen");
		return -1;
	}
	FILE *fp1=fopen("2.txt","w");
	if(NULL==fp1){
		printf("__%d__\n",__LINE__);
		perror("fopen");
		return -1;
	}

	
	char c = 0;
	while(1){
		c=fgetc(fp);
		if(EOF==c)
			break;
		fputc(c,fp1);
	}
	if(fclose(fp)<0){
		perror("fclose");
		return -1;
	}
	FILE *fp2=fopen("1.txt","r");
	if(NULL==fp2){
		printf("__%d__\n",__LINE__);
		perror("fopen");
		return -1;
	}
	int count = 0;
	int count1 = 0;
	while(1){
		c = fgetc(fp);
		if(EOF==c)
			break;
		printf("%c",c);
		if('\n'==c)
			count1++;
		count++;
	}
	printf("个数=%d\t行数=%d\n",count,count1);

	if(fclose(fp2)<0){
		printf("__%d__\n",__LINE__);
		perror("fclose");
		return -1;
	}
	if(fclose(fp1)<0){
		printf("__%d__\n",__LINE__);
		perror("fclose");
		return -1;
	}
	return 0;
}

7.25 IO fprintf fscanf fputc fgetc_第5张图片

你可能感兴趣的:(c语言)