设置流缓冲区

版权声明:交个朋友呗,QQ1085081059 https://blog.csdn.net/guozhikai2019/article/details/88921317
 setbuf(fp,NULL);

第一个参数为要操作的流对象,第二个参数为buf必须指向长度为BUFSIZ的缓冲区。

如果buf设置为NULL,则关闭缓冲区。

setvbuf( fp , NULL, _IONBF , 0 );

setvbuf( fp , buf , _IOLBF , sizeof(buf) );

setvbuf( fp , buf , _IOFBF , sizeof(buf) );

如果指定全缓冲或行缓冲,则buf和size可以选择指定一个缓冲区及其长度(大于等于128字节)。

如果指定改流是带缓冲区的,而buf是NULL,则标准IO自动为该流分配适当长度的缓冲区。

/* If BUF is NULL, make STREAM unbuffered.
   Else make it use buffer BUF, of size BUFSIZ.  */
extern void setbuf (FILE *__restrict __stream, char *__restrict __buf) __THROW;
/* Make STREAM use buffering mode MODE.
   If BUF is not NULL, use N bytes of it for buffering;
   else allocate an internal buffer N bytes long.  */
extern int setvbuf (FILE *__restrict __stream, char *__restrict __buf,
		    int __modes, size_t __n) __THROW;
/* Example show usage of setbuf() &setvbuf() */
#include<stdio.h>
#include<error.h>
#include<string.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
    //使用只写的方式打卡文件no_buf1.txt,如果文件不存在则创建文件,如果文件存在,则清空
    if(( fp = fopen("no_buf1.txt","w")) == NULL)
    {
        perror("file open failure!");
        return(-1);
    }
    //设置写no_buf1.txt文件为无缓冲(如果不设置,则操作文件为全缓冲)
    setbuf(fp,NULL);
    memset(buf,'\0',128);
    //执行完fwrite后no_buf1.txt的内容为hello,w ,前面已经设置写文件的方式为无缓冲
    //msg1:从这里面取数据,单个数据的字节数为7字节,取1个数据,文件指针为fp
    fwrite( msg1 , 7 , 1 , fp );
    printf("test setbuf(no buf)!check no_buf1.txt\n");
    printf("now buf data is :buf=%s\n",buf);

    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);
    }

    //文件no_buf2.txt也是使用无缓冲,模式为_IONBF时,第2,4个参数忽略
    setvbuf( fp , NULL, _IONBF , 0 );
    memset(buf,'\0',128);
    //调用完此句话后,文件的内容为hello,w
    fwrite( msg1 , 7 , 1 , fp );
    printf("test setvbuf(no buf)!check no_buf2.txt\n");

    printf("now buf data is :buf=%s\n",buf);

    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);
    }
    //设置为行缓冲,缓冲区为buf,缓冲区大小为buf长度
    setvbuf( fp , buf , _IOLBF , sizeof(buf) );
    memset(buf,'\0',128);
    //msg2:"hello\nworld",再调用fclose之前只有hello被写进去
    fwrite( msg2 , strlen(msg2) , 1 , fp );
    printf("test setvbuf(line buf)!check l_buf.txt, because line buf ,only data before enter send to file\n");

    printf("now buf data is :buf=%s\n",buf);
    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);
    }
    //设置为全缓冲,缓冲区为buf,缓冲区大小为sizeof(buf)
    setvbuf( fp , buf , _IOFBF , sizeof(buf) );
    memset(buf,'\0',128);
    fwrite( msg2 , strlen(msg2) , 1 , fp );
    printf("test setbuf(full buf)!check f_buf.txt\n");

    printf("now buf data is :buf=%s\n",buf);
    printf("press enter to continue!\n");
    getchar();

    fclose(fp);

}

猜你喜欢

转载自blog.csdn.net/guozhikai2019/article/details/88921317
今日推荐