C examples for stdio.h:setbuf
function
<cstdio> <stdio.h>
Specifies buffer form the stream for I/O operations.
void setbuf ( FILE * stream, char * buffer );
Parameter | Description |
---|---|
stream | FILE object. |
buffer | User allocated buffer. |
none.
In the following code, two files are opened for writing.
myfile1.txt is set to a user buffer until the fflush function is called.
myfile2.txt is set to unbuffered, so the subsequent output operation is written to the device as soon as possible.
closing a file flushes its buffer.
#include <stdio.h> int main ()//from ww w. j av a2 s .c o m { char buffer[BUFSIZ]; FILE *pFile1, *pFile2; pFile1=fopen ("myfile1.txt","w"); pFile2=fopen ("myfile2.txt","a"); setbuf ( pFile1 , buffer ); fputs ("This is sent to a buffered stream",pFile1); fflush (pFile1); setbuf ( pFile2 , NULL ); fputs ("This is sent to an unbuffered stream",pFile2); fclose (pFile1); fclose (pFile2); return 0; }