The function setvbuf () in C
The function int setvbuf (FILE * stream, char * buffer, int mode, size_t size) in Standard C Library determines how a Stream should be buffered.
Declare setvbuf () function in C
Below is the declaration for the setvbuf () function in C:
int setvbuf ( FILE * stream , char * buffer , int mode , size_t size )
Parameters
stream - This is the pointer to a FILE object that identifies the opened stream.
buffer - This is the buffer allocated. If set to NULL, the function automatically allocates a Buffer with the specified size.
mode - Defines the mode for buffering files.
mode Description_IOFBF Full buffering - On the output, the data is written once the buffer is full. On the input, the buffer is filled when an input operation is requested and the buffer is empty _IOLBF Line buffering - On the output, data is recorded when encountered: or a newline character (new line) is inserted into Stream or when the buffer is full . On the Input, the buffer is filled in to the newline character next to the input when an input operation is requested and the buffer is empty _IONBF No buffering - No buffers are used. Each I / O activity is recorded as soon as possible. Buffer parameters and size are ignoredsize - This is the buffer size (byte value).
Returns the value
This function returns 0 if successful. If not, the function returns a value other than 0.
For example
The following program C illustrates how to use the setvbuf () function in C:
#include #include int main () { char buff [ 1024 ]; memset ( buff , '' , sizeof ( buff )); fprintf ( stdout , "Chuan bi buffern" ); setvbuf ( stdout , buff , _IOFBF , 1024 ); fprintf ( stdout , "Hoc C co ban va nang cao tai QTM !!!n" ); fprintf ( stdout , "Phan noi dung nay se duoc demn" ); fflush ( stdout ); return ( 0 ); }
Compile and run the above program to produce the following results. Here the program still buffers the output into the buff until it encounters the first call to fflush (), then it starts buffering output.
According to Tutorialspoint
Previous lesson: Function setbuf () in C
Next lesson: Function tmpfile () in C
You should read it
May be interested
- Function tmpfile () in Cfunction file * tmpfile (void) in standard c library create temporary files in wb + mode. the temporary file created will be automatically deleted when the stream is closed (fclose function) or when the program ends.
- Function tmpnam () in Cthe function char * tmpnam (char * str) in the c library standard creates and returns a valid temporary file name (temp file) that did not exist before creation. if str is null, it returns the tmp file name.
- Function fprintf () in Cthe int fprintf function (file * stream, const char * format, ...) in standard c library sends formatted output to a stream.
- The function gets () in Cthe char * gets (char * str) function in standard c library reads a line from stdin and stores it inside the string pointed by str. it stops when the end-of-file or newline character is encountered (new line) is read.
- The sprintf () function in Cthe int sprintf function (char * str, const char * format, ...) in the standard c library sends the formatted output to a string str.
- Function vfprintf () in Cthe int vfprintf function (file * stream, const char * format, va_list arg) in the standard c library sends the formatted output to a stream using a parameter list.