[Linux流操作]查看三种标准流的缓冲方式
#include<stdio.h>#include<stdlib.h>#ifdefined(MACOS)#define_IO_UNBUFFERED__SNBF#define_IO_LINE_BUF__SLBF#define_IO_file_flags_flags#defineBUFFERSZ(fp)(fp)->_bf._size#else#defineBUFFERSZ(fp)((fp)->_IO_buf_end-(fp)->_IO_buf_base)#endifvoidpr_stdio(constchar*,FILE*);intmain(void){FILE*fp;printf("plsentersomestr\n");//提示输入部分字符if(getchar()==EOF)//如果EOF错误{perror("getcharerror");}fputs("onelinetostandarderror\n",stderr);pr_stdio("stdin",stdin);pr_stdio("stdout",stdout);pr_stdio("stderr",stderr);//输出相应的缓冲类型perror("fopenerror");if(getc(fp)==EOF)perror("getcerror");pr_stdio("/etc/motd",fp);exit(0);}//缓冲输出函数voidpr_stdio(constchar*name,FILE*fp){printf("stream=%s,",name);//打印缓冲名称if(fp->_IO_file_flags&_IO_UNBUFFERED)printf("unbuffered");elseif(fp->_IO_file_flags&_IO_LINE_BUF)printf("linebuffered");else/*ifneitherofabove*/printf("fullybuffered");printf(",buffersize=%ld\n",BUFFERSZ(fp));}
//这是一个分别打印三个标准流和一个文件流//的缓冲方式的应用实例#include<stdio.h>#include<stdlib.h>#ifdefined(MACOS)#define_IO_UNBUFFERED__SNBF#define_IO_LINE_BUF__SLBF#define_IO_file_flags_flags#defineBUFFERSZ(fp)(fp)->_bf._size#else#defineBUFFERSZ(fp)((fp)->_IO_buf_end-(fp)->_IO_buf_base)#endif//以上是关于缓冲方式和缓冲区大小的预定义voidpr_stdio(constchar*,FILE*);//子函数声明intmain(intargc,char*argv[]){FILE*fp;//流文件结构指针pr_stdio("stdin",stdin);//标准输入pr_stdio("stdout",stdout);//标准输出pr_stdio("stderr",stderr);//标准出错处理printf("fopenerror");if(getc(fp)==EOF){printf("getcerror\n");}return0;}//测试缓冲输出函数voidpr_stdio(constchar*name,FILE*fp){printf("当前流是%s,",name);//打印流的名称if(fp->_IO_file_flags&_IO_UNBUFFERED){printf("无缓冲\n");}elseif(fp->_IO_file_flags&_IO_LINE_BUF){printf("行缓冲\n");}else{printf("全缓冲\n");}printf(",缓冲区大小=%ld\n",BUFFERSZ(fp));return;}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。