⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 setbuf_example.c

📁 linux下的程序开发(C语言实现):本程序主要实现文件的操作
💻 C
字号:
/* Example show usage of setbuf() &setvbuf() */#include<stdio.h>#include<error.h>int main( int argc , char ** argv ){	int i;	FILE * fp;	char msg1[]="hello,wolrd\n";	char msg2[] = "hello\nworld";	char buf[128];//open a file and set nobuf(used setbuf).and write string to it,check it before close of flush the stream	if(( fp = fopen("no_buf1.txt","w")) == NULL)	{		perror("file open failure!");		return(-1);	}	setbuf(fp,NULL);	fwrite( msg1 , 7 , 1 , fp );	printf("test setbuf(no buf)!check no_buf1.txt\n");	printf("press enter to continue!\n");	getchar();	fclose(fp);//open a file and set nobuf(used setvbuf).and write string to it,check it before close of flush the stream	if(( fp = fopen("no_buf2.txt","w")) == NULL)	{		perror("file open failure!");		return(-1);	}	setvbuf( fp , NULL, _IONBF , 0 );	fwrite( msg1 , 7 , 1 , fp );	printf("test setvbuf(no buf)!check no_buf2.txt\nbecause line buf, only before enter data write\n");	printf("press enter to continue!\n");	getchar();	fclose(fp);//open a file and set line buf(used setvbuf).and write string(include '\n') to it,////check it before close of flush the stream	if(( fp = fopen("l_buf.txt","w")) == NULL)	{		perror("file open failure!");		return(-1);	}	setvbuf( fp , buf , _IOLBF , sizeof(buf) );	fwrite( msg2 , sizeof(msg2) , 1 , fp );	printf("test setvbuf(line buf)!check l_buf.txt, because line buf ,only data before enter send to file\n");	printf("press enter to continue!\n");	getchar();	fclose(fp);//open a file and set full buf(used setvbuf).and write string to it for 20th time (it is large than the buf)//check it before close of flush the stream	if(( fp = fopen("f_buf.txt","w")) == NULL){		perror("file open failure!");		return(-1);	}	setvbuf( fp , buf , _IOFBF , sizeof(buf) );		fwrite( msg2 , sizeof(msg2) , 1 , fp );	/*	for(i = 0 ; i <10 ; i ++ )	{		fputs( msg1 , fp );	}*/	printf("test setbuf(full buf)!check f_buf.txt\n");	printf("press enter to continue!\n");	getchar();	fclose(fp);	}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -