Clear, practical technology insights About · Contact

Function setbuf () in C

The function void setbuf (FILE * stream, char * buffer) in the standard C Library defines how a Stream is buffered. This function should be called once the file attached to the Stream has been opened, but before any input or output operation has taken place.

Author: David Pac2 minutes read
Table of Contents

The function void setbuf (FILE * stream, char * buffer) in the standard C Library defines how a Stream is buffered. This function should be called once the file attached to the Stream has been opened, but before any input or output operation has taken place.

Declare setbuf () function in C

Below is the declaration for the setbuf () function in C:

 void  setbuf ( FILE  * stream , char * buffer ) 

Parameters

stream - This is the pointer to a FILE object that identifies the opened stream.

buffer - This is the buffer that has been allocated. It should be at least BUFSIZ bytes in length, which is a macro constant to be used as the length of this array.

Returns the value

This function does not return any values.

For example

The following program C illustrates the usage of the setbuf () function in C:

 #include int  main () { char  buf [ BUFSIZ ];  setbuf ( stdout ,  buf );  puts ( "Hoc C co ban tai QTM !!!" );  fflush ( stdout ); return ( 0 ); } 

Compile and run the above program to produce the following results. Here the program sends output to STDOUT just before it exits, otherwise it continues to buffer output. You can also use the fflush () function to flush (delete or move to the periphery) to see the result.

According to Tutorialspoint

Previous lesson: The rewind () function in C

Next lesson: Function setvbuf () in C

Was this article helpful?

Your feedback helps us improve.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.