1 标准IO

linux下一切皆文件!      

标准IO (基于有缓存的IO)eg:printf  fgetc  fputc

文件IO (基于无缓存的IO)eg:write   read

 

linux文件类型:

b/block :块设备文件

c/character:字符设备文件

d/directory:目录文件

-:普通文件

l/link:链接文件(软连接、符号链接)

s/socket:套接字文件

p/pipe:管道文件

===================================================================

setvbuf.c

 1 #include <stdio.h>
 2 
 3 int main(void)
 4 {
 5     char buf[4096];
 6 //    char buf[1024];
 7 
 8     setvbuf(stdout, buf, _IOFBF, sizeof(buf));
 9 //    setvbuf(stdout, NULL, _IONBF, 0);
10     printf("hello world!");
11 
12 //    while (1);
13 
14     return 0;
15 }
16 /*
17  *int setvbuf(FILE *stream, char *buf, int mode, size_t size);
18  *功能:改变缓存的类型,用buf指向的空间来代替现有的缓存区
19  *参数:文件流指针/新缓存区首地址
20          /缓存模式:1:_IOFBF(全缓存)
21                    2:_IOLBF(行缓存)
22                    3:_IONBF(不换存)
23         /新缓存区的大小
24  *返回值:成功返回0;失败返回非0
25  * */

fgetc.c

 1 #include <stdio.h>
 2 int main(void)
 3 {
 4     FILE *fp = NULL;
 5     char ret = 0;
 6     if(NULL==(fp = fopen("file_fgetc.txt","r"))) {
 7         perror("fail to fopen!");
 8         return -1;
 9     }
10 //从文件中读取数据并打印
11     while((ret = fgetc(fp))!=EOF) {
12         //printf("ret = %c\n",ret);
13         printf("%c ",ret);
14     }
15     fclose(fp);
16     return 0;
17 }
18 /*
19  *int fgetc(FILE *stream);      ==>getchar()=fgetc(stdin);
20  *功能:从流中读取一个字符
21  *返回值:成功返回读取到的字符的ASCII码值;失败返回EOF/读到文件末尾返回EOF
22  *
23  *FILE *fopen(const char *path, const char *mode);
24  *功能:打开一个文件,获取一个文件流指针
25  *参数:打开文件的路径/打开模式:
26      1.r: 不存在则报错;存在则只读打开
27      2.r+:不存在则报错;存在则读写打开
28     3.w:  不存在则创建;存在则清空只写打开
29     4.w+: 不存在则创建;存在则清空写读打开
30     5.a:  不存在则创建;存在则追加只写打开
31     6.a+: 不存在则创建;存在则追加写读打开
32  *返回值:成功返回文件流指针;失败返回NULL
33  *
34  * */

file_fgetc.c

 1 hello world!abc 

fputc.c

 1 #include <stdio.h>
 2 int main(void)
 3 {
 4     FILE *fp = NULL;
 5     char ret = 0;
 6     if(NULL==(fp = fopen("file_fputc.txt","w"))) {
 7         perror("fail to fopen!");
 8         return -1;
 9     }
10     fputc('I',fp);
11     fputc('l',fp);
12     fputc('o',fp);
13     fputc('v',fp);
14     fputc('e',fp);
15     fputc('y',fp);
16     fputc('o',fp);
17     fputc('u',fp);
18 
19     fclose(fp);
20     return 0;
21 }
22 /*
23  *int fputc(int c, FILE *stream);      ==> putchar()=fputc(stdout);
24  *功能:向流中写入一个字符
25  *参数:字符的ASCII码值/文件流指针
26  *返回值:成功返回写入字符的ASCII码值;失败返回EOF
27  *
28  * */

file_fputc.c

 1 Iloveyou 

=====================================================================

fputs.c

 1 #include <stdio.h>
 2 int main(void)
 3 {
 4 #if 0
 5     puts("hello world!");//在输入字符串末尾加\n
 6     puts("i love you!");
 7 #endif
 8 #if 1
 9     fputs("hello word!",stdout);//直接将字符串输出不加\n
10     fputs("i love you!",stdout);
11 #endif

   FILE *fp = NULL;
   if(NULL == (fp = fopen("file_fputs.c","w"))) {
      perror("fail to fopen!\n");
   }
    fputs("hello gaga!",fp);
    fputs("33333333333",fp);

12     return 0;
13 }
14 
15 /*
16  *int fputs(const char *s, FILE *stream);   ==> fputs(s, stdout) = puts(s);//等价关系
17  *功能:向流中写入一串字符                异同:fputs:直接将字符串输出,不加'\n'或'\0'        
18  *参数:字符串首地址/文件流指针                puts:在输入字符串末尾加'\n'
19  *返回值:成功返回非负数;失败返回EOF
20  *
21  * */

fgets.c

 1 #include <stdio.h>
 2 int main(void)
 3 {
 4     FILE *fp = NULL;
 5     char ret = 0;
 6     if(NULL==(fp = fopen("file_fgets.txt","r"))) {
 7         perror("fail to fopen!");
 8         return -1;
 9     }
10     char buff[1024]={0};
11     fgets(buff,sizeof(buff),fp);//从文件中读取数据到 buff
12                                 //fgets自带\n;不会去掉从终端读取的\n
13         printf("%s\n",buff);
14     fgets(buff,sizeof(buff),fp);//从文件中读取数据到 buff
15         printf("%s\n",buff);
16     fgets(buff,sizeof(buff),fp);//从文件中读取数据到 buff
17         printf("%s\n",buff);
18 
19     fclose(fp);
20     return 0;
21 }
22 
23 /*
24  *char *fgets(char *s, int size, FILE *stream);
25  *功能:从流中读取一字符串,最少读1个,最多读size个字节,遇到EOF或者'\n'停止读取,每次读取字符串末尾要加'\0'
26  *参数:读取到数据存放新空间的首地址/数据存放空间的大小/文件流指针
27  *返回值:成功返回存放空间的首地址;失败或者读到文件末尾返回NULL
28  *
29  *gets和fgets异同:
30  *1.gets不限定从终端读取字符的个数,如果超过最大保存字符个数,数据会溢出,容易产生漏洞/会去掉。。。下
31  *2.fgets限定从终端读取字符的个数,不会产生溢出错误/不会去掉从终端读取的'\n'字符
32  *
33  *
34  * */

file_fgets.txt

 1 hello world! 2 i love you! 

gets.c

 1 #include <stdio.h>
 2 int main(void)
 3 {
 4     char buff[1024]={0};
 5     gets(buff);//获取 buff
 6                 //会去掉从终端读取的\n
 7 //    fgets(buff,sizeof(buff),stdin);//与上等价
 8         printf("%s\n",buff);
 9 
10     return 0;
11 }
12 
13 /*
14  *
15  * */

fwrite.c

 1 #include "head.h"
 2 int main(void)
 3 {
 4     FILE*fp = NULL;
 5     struct student s1 = {"gemeng",100421104,'m',24};
 6     struct student s2 = {"heheda",100421105,'f',23};
 7     if(NULL == (fp = fopen("info.txt","w"))) {
 8         perror("fail to fopen!");
 9         return -1;
10     }
11     fwrite(&s1,sizeof(struct student),1,fp);//从指定地址/buff 读取数据到fp
12     fwrite(&s2,sizeof(struct student),1,fp);
13 
14     fclose(fp);
15     return 0;
16 }
17 /*
18  *size_t fwrite(const void *ptr, size_t size, size_t nmemb,FILE *stream);
19  *功能:向流中写入nmemb个数据对象,每个对象大小为size,对象以前存放地址为ptr
20  *返回值:成功返回实际写入的数据对象的个数;失败返回0
21  *
22  * */

fread.c

 1 #include "head.h"
 2 int main(void)
 3 {
 4     FILE*fp = NULL;
 5     struct student a1;
 6     struct student a2;
 7     if(NULL == (fp = fopen("info.txt","r"))) {
 8         perror("fail to fopen!");
 9         return -1;
10     }
11     int ret = 0;
12     ret = fread(&a1,sizeof(struct student),1,fp);//从fp读取数据到指定地址/buff
13     printf("ret:%d\n",ret);
14     printf("name:%s\n",a1.name);
15     printf("num:%d\n",a1.num);
16     printf("sex:%c\n",a1.sex);
17     printf("age:%d\n",a1.age);
18     ret = fread(&a2,sizeof(struct student),1,fp);
19     printf("ret:%d\n",ret);
20     printf("name:%s\n",a2.name);
21     printf("num:%d\n",a2.num);
22     printf("sex:%c\n",a2.sex);
23     printf("age:%d\n",a2.age);
24 
25     fclose(fp);
26     return 0;
27 }
28 /*
29  * size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);
30  * 功能:从流中读取nmemb个对象,每个对象size大小,存放到ptr指向的空间地址
31  * 返回值:成功返回读取对象的个数;失败或者读到文件末尾返回0
32  *
33  * */

copy_1.c

 1 #include <stdio.h>
 2 int main(char argc,const char *argv[])
 3 {
 4     if(argc != 3) {
 5         fprintf(stderr,"Usage:./a.out 源文件 目的文件\n");
 6             return -1;
 7     }
 8     FILE *fsrc = NULL;
 9     FILE *fdst = NULL;
10     fsrc = fopen(argv[1],"r");
11     fdst = fopen(argv[2],"w");
12     if((fsrc == NULL)|(fdst == NULL)) {
13         perror("fail to fopen!");
14         return -1;
15     }
16     char ret = 0;
17     /*利用fgetc和fputc来实现拷贝功能*/
18     while((ret = fgetc(fsrc)) != EOF) {
19         fputc(ret,fdst);
20     }
21 
22     fclose(fsrc);
23     fclose(fdst);
24 
25     return 0;
26 }

copy_2.c

 1 #include <stdio.h>
 2 int main(char argc,const char *argv[])
 3 {
 4     if(argc != 3) {
 5         fprintf(stderr,"Usage:./a.out 源文件 目的文件\n");
 6             return -1;
 7     }
 8     FILE *fsrc = NULL;
 9     FILE *fdst = NULL;
10     fsrc = fopen(argv[1],"r");
11     fdst = fopen(argv[2],"w");
12     if((fsrc == NULL)|(fdst == NULL)) {
13         perror("fail to fopen!");
14         return -1;
15     }
16 //    char ret = 0;
17     char buff[1024] = {0};
18     /*利用fgets和fputs来实现拷贝功能*/
19     while((NULL != fgets(buff,sizeof(buff),fsrc))) {
20         fputs(buff,fdst);
21     }
22 
23     fclose(fsrc);
24     fclose(fdst);
25 
26     return 0;
27 }

copy_3.c

 1 #include <stdio.h>
 2 int main(char argc,const char *argv[])
 3 {
 4     if(argc != 3) {
 5         fprintf(stderr,"Usage:./a.out 源文件 目的文件\n");
 6             return -1;
 7     }
 8     FILE *fsrc = NULL;
 9     FILE *fdst = NULL;
10     fsrc = fopen(argv[1],"r");
11     fdst = fopen(argv[2],"w");
12     if((NULL == fsrc)|(NULL == fdst)) {
13         perror("fail to fopen!");
14         return -1;
15     }
16     int ret = 0;
17     char buff[1024] = {0};
18     /*利用fread和fwrite来实现拷贝功能*/
19     while((ret = fread(buff,sizeof(char),1024,fsrc)) != 0) {
20         fwrite(buff,sizeof(char),ret,fdst);
21     }
22 
23     fclose(fsrc);
24     fclose(fdst);
25 
26     return 0;
27 }

 

posted @ 2017-03-15 16:14  bkycrmn  阅读(158)  评论(0)    收藏  举报