Latest web development tutorials

C library functions - setvbuf ()

C standard library - <stdio.h> C standard library - <stdio.h>

description

C library functionsint setvbuf (FILE * stream, char * buffer, int mode, size_t size) to define how the flow stream buffer.

statement

Here is () statement setvbuf function.

int setvbuf(FILE *stream, char *buffer, int mode, size_t size)

parameter

  • stream - This is a pointer to FILE pointer to an object, the object identifier FILE an open stream.
  • buffer - which is assigned to the user buffer.If set to NULL, the function will automatically assign a specified buffer size.
  • mode - This specifies the file buffer mode:
模式描述
_IOFBF全缓冲:对于输出,数据在缓冲填满时被一次性写入。对于输入,缓冲会在请求输入且缓冲为空时被填充。
_IOLBF行缓冲:对于输出,数据在遇到换行符或者在缓冲填满时被写入,具体视情况而定。对于输入,缓冲会在请求输入且缓冲为空时被填充,直到遇到下一个换行符。
_IONBF无缓冲:不使用缓冲。每个 I/O 操作都被即时写入。buffer 和 size 参数被忽略。
  • size - this is the buffer size in bytes.

return value

If successful, the function returns 0, otherwise it returns a nonzero value.

Examples

The following example demonstrates setvbuf () function is used.

#include <stdio.h>

int main()
{

   char buff[1024];

   memset( buff, '\0', sizeof( buff ));

   fprintf(stdout, "启用全缓冲\n");
   setvbuf(stdout, buff, _IOFBF, 1024);

   fprintf(stdout, "这里是 w3cschool.cc\n");
   fprintf(stdout, "该输出将保存到 buff\n");
   fflush( stdout );

   fprintf(stdout, "这将在编程时出现\n");
   fprintf(stdout, "最后休眠五秒钟\n");

   sleep(5);

   return(0);
}

Let's compile and run the above program, which will produce the following results. Here, a program to save the output buffer tobuff, until the first call to fflush ()before starting a buffered output of the last 5 seconds of sleep. It will be before the end of the program, to send the remaining output to STDOUT.

启用全缓冲
这里是 w3cschool.cc
该输出将保存到 buff
这将在编程时出现
最后休眠五秒钟

C standard library - <stdio.h> C standard library - <stdio.h>