22.11.7 IO day1

 1.fopen    函数内打开

ubuntu@ubuntu:~/yuyu/yu/1/3$ gcc 4.c
ubuntu@ubuntu:~/yuyu/yu/1/3$ ./a.out
fopen failed
ubuntu@ubuntu:~/yuyu/yu/1/3$ ls
1.c  3.c  a.out     fopen2.c  fopen.c
2.c  4.c  fopen1.c  fopen3.c  usr.txt
ubuntu@ubuntu:~/yuyu/yu/1/3$ touch fopen.txt
ubuntu@ubuntu:~/yuyu/yu/1/3$ ls
1.c  3.c  a.out     fopen2.c  fopen.c    usr.txt
2.c  4.c  fopen1.c  fopen3.c  fopen.txt
ubuntu@ubuntu:~/yuyu/yu/1/3$ gcc 4.c
ubuntu@ubuntu:~/yuyu/yu/1/3$ ./a.out
fopen success
ubuntu@ubuntu:~/yuyu/yu/1/3$ cat 4.c
#include <stdio.h>

int main(int argc, const char *argv[])
{
	FILE* fp = fopen("./fopen.txt","r");
	if(NULL==fp)
	{
		printf("fopen failed\n");
		return -1;
	}

	printf("fopen success\n");
	return 0;
}
ubuntu@ubuntu:~/yuyu/yu/1/3$ 

2.fopen fclose  文件打开文件关闭

buntu@ubuntu:~/yuyu/yu/1/3$ gcc 5.c
ubuntu@ubuntu:~/yuyu/yu/1/3$ ./a.out
fopen success
fclose success
ubuntu@ubuntu:~/yuyu/yu/1/3$ cat 5.c
#include <stdio.h>
#include <errno.h>

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

	printf("fopen success\n");

	//关闭文件
	if(fclose(fp)<0)
	{
		perror("fclose");
		return -1;
	}
	printf("fclose success\n");
	return 0;
}
ubuntu@ubuntu:~/yuyu/yu/1/3$ 

 3.在文件中输入一个字符串

ubuntu@ubuntu:~/yuyu/yu/1/3$ gcc 6.c
ubuntu@ubuntu:~/yuyu/yu/1/3$ ./a.out
hello world 10
fprintf success
ubuntu@ubuntu:~/yuyu/yu/1/3$ cat 6.c
#include <stdio.h>
int main(int argc, const char *argv[])
{
	//打开一个文件夹
	FILE *fp = fopen("./fprintf.txt","w");
	if(NULL == fp)
	{
		perror("fopen");
		return -1;
	}

	//将数据格式化输出到文件中
	printf("hello world %d\n",10);
	if(fprintf(fp,"hello world %d\n",10)<0)
	{
		printf("fprintf failed\n");
		return -1;
	}
	printf("fprintf success\n");


	//关闭
	fclose(fp);
	return 0;
}
ubuntu@ubuntu:~/yuyu/yu/1/3$
ubuntu@ubuntu:~/yuyu/yu/1/3$ cat fprintf.txt
hello world 10

4.读取文件内容

ubuntu@ubuntu:~/yuyu/yu/1/3$ gcc 7.c
7.c: In function ‘main’:
7.c:19:18: warning: format ‘%d’ expects argument of type ‘int *’, but argument 3 has type ‘char *’ [-Wformat=]
   if(fscanf(fp,"%d",&brr)<0)
                 ~^  ~~~~
                 %hhd
ubuntu@ubuntu:~/yuyu/yu/1/3$ gcc 7.c
7.c: In function ‘main’:
7.c:19:18: warning: format ‘%d’ expects argument of type ‘int *’, but argument 3 has type ‘int’ [-Wformat=]
   if(fscanf(fp,"%d",brr)<0)
                 ~^
ubuntu@ubuntu:~/yuyu/yu/1/3$ gcc 7.c
7.c: In function ‘main’:
7.c:19:18: warning: format ‘%d’ expects argument of type ‘int *’, but argument 3 has type ‘int’ [-Wformat=]
   if(fscanf(fp,"%d",brr)<0)
                 ~^
ubuntu@ubuntu:~/yuyu/yu/1/3$ gcc 7.c
7.c: In function ‘main’:
7.c:19:18: warning: format ‘%d’ expects argument of type ‘int *’, but argument 3 has type ‘int’ [-Wformat=]
   if(fscanf(fp,"%d",brr)<0)
                 ~^
ubuntu@ubuntu:~/yuyu/yu/1/3$ gcc 7.c
7.c: In function ‘main’:
7.c:19:18: warning: format ‘%c’ expects argument of type ‘char *’, but argument 3 has type ‘int’ [-Wformat=]
   if(fscanf(fp,"%c",brr)<0)
                 ~^
ubuntu@ubuntu:~/yuyu/yu/1/3$ gcc 7.c
ubuntu@ubuntu:~/yuyu/yu/1/3$ ./a.out
104
101
108
108
111
32
119
111
114
108
100
32
49
48
10
文件读取完毕

ubuntu@ubuntu:~/yuyu/yu/1/3$ ls
1.c  4.c  7.c    fopen1.c  fopen.c      usr.txt
2.c  5.c  a.out  fopen2.c  fopen.txt
3.c  6.c  core   fopen3.c  fprintf.txt
ubuntu@ubuntu:~/yuyu/yu/1/3$ vi fprintf.txt
ubuntu@ubuntu:~/yuyu/yu/1/3$ cat fprintf.txt
hello world 10
ubuntu@ubuntu:~/yuyu/yu/1/3$ gcc 7.c
ubuntu@ubuntu:~/yuyu/yu/1/3$ ./a.out
104	   h
101	   e
108	   l
108	   l
111	   o
32	    
119	   w
111	   o
114	   r
108	   l
100	   d
32	    
49	   1
48	   0
10	   

文件读取完毕

ubuntu@ubuntu:~/yuyu/yu/1/3$ cat 7.c
#include <stdio.h>
#include <errno.h>

int main(int argc, const char *argv[])
{
	//打开一个文件:以读的方式打开
	FILE* fp = fopen("./fprintf.txt","r");
	if(NULL==fp)	
	{
		perror("fopen");
		return -1;
	}

//	int brr[20] = {0};
//	int i = 0;
	char brr;
	while(1)
	{
		if(fscanf(fp,"%c",&brr)<0)
		{
			if(0 == errno)
			{
				printf("文件读取完毕\n");
				break;
			}
			else
			{
				perror("fscanf");
				return -1;
			}
		}
		printf("%d\t  %2c",brr ,brr);
		putchar(10);
			
	}
	printf("\n");

	//关闭
	fclose(fp);
    	return 0;
}


5. 用户登录

ubuntu@ubuntu:~/yuyu/yu/1/3$ ls
1.c  4.c  7.c    fopen1.c  fopen.c      usr.txt
2.c  5.c  a.out  fopen2.c  fopen.txt
3.c  6.c  core   fopen3.c  fprintf.txt
ubuntu@ubuntu:~/yuyu/yu/1/3$ gcc fopen2.c
ubuntu@ubuntu:~/yuyu/yu/1/3$ ./a.out
请输入账户名和密码>>>>>> 张三 12345
李四 abcde
fname = 张三 fpasswd = 12345
fname = 李四 fpasswd = abcde
文件读取完毕
用户存在
ubuntu@ubuntu:~/yuyu/yu/1/3$ cat fopen2.c
#include <stdio.h>
#include <errno.h>
#include <string.h>

int main(int argc, const char *argv[])
{
	//打开一个文件:以读的方式打开
	FILE* fp =fopen("./usr.txt","r");
	if(NULL==fp)
	{
		perror("fopen");
		return -1;
	}

	//从终端获取一个账户名和密码
	char name[20],passwd[20];
	printf("请输入账户名和密码>>>>>>");
	if(scanf("%s %s", name, passwd)!=2)
	{
		perror("scanf");
		return -1;
	}

	//将终端的name和passwd 循环与文件中存储的账户和密码进行比较
	char fname[20],fpasswd[20];
	int flag = 0;
	while(1)
	{
		if(fscanf(fp,"%s %s",fname,fpasswd)<0)
		{
			if(0==errno)
			{
				printf("文件读取完毕\n");
				flag = 1;
				break;
			}
			else
			{
				perror("fscanf");
				return -1;
			}
		}
		printf("fname = %s fpasswd = %s\n", fname, fpasswd);

		//将文件中读取到的账户和密码与终端获取的账户密码比较
		if(strcmp(name, fname) != 0)
		{
			continue;
		}

		//能运行到当前位置,则代表用户名相同,则去比较密码
		if(strcmp(passwd,fpasswd)==0)
		{
			printf("登陆成功\n");
		}
		else
		{
			printf("登录错误\n");
		}

		//只要用户名相同,则代表找到了该用户,
		//无所谓密码正确不正确,可以直接退出循环
		break;

	}
	if(1 == flag)
		printf("用户存在\n");


	//关闭
	fclose(fp);

	return 0;
}
ubuntu@ubuntu:~/yuyu/yu/1/3$ 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值