| << Prev | Beej's Guide to C | Next >> |
Configure buffering for standard I/O operations
#include <stdio.h> void setbuf(FILE *stream, char *buf); int setvbuf(FILE *stream, char *buf, int mode, size_t size);
Now brace yourself because this might come as a bit of a surprise to
you: when you printf() or fprintf() or use any
I/O functions like that, it does not normally work
immediately. For the sake of efficiency, and to irritate you, the
I/O on a
So what are the different buffering behaviors? The biggest is called "full buffering", wherein all I/O is stored in a big buffer until it is full, and then it is dumped out to disk (or whatever the file is). The next biggest is called "line buffering"; with line buffering, I/O is stored up a line at a time (until a newline ('\n') character is encountered) and then that line is processed. Finally, we have "unbuffered", which means I/O is processed immediately with every standard I/O call.
You might have seen and wondered why you could call putchar() time and time again and not see any output until you called putchar('\n'); that's right--stdout is line-buffered!
Since setbuf() is just a simplified version of setvbuf(), we'll talk about setvbuf() first.
The stream is the
The next argument, buf allows you to make your own buffer
space (using malloc() or just a
Now we get to the real meat of the function: mode allows you to choose what kind of buffering you want to use on this stream. Set it to one of the following:
stream will be fully buffered.
stream will be line buffered.
stream will be unbuffered.
Finally, the size argument is the size of the array you
passed in for buf...unless you passed
Now what about this lesser function setbuf()? It's just like calling setvbuf() with some specific parameters, except setbuf() doesn't return a value. The following example shows the equivalency:
// these are the same: setbuf(stream, buf); setvbuf(stream, buf, _IOFBF, BUFSIZ); // fully buffered // and these are the same: setbuf(stream, NULL); setvbuf(stream, NULL, _IONBF, BUFSIZ); // unbuffered
setvbuf() returns zero on success, and nonzero on failure. setbuf() has no return value.
FILE *fp;
char lineBuf[1024];
fp = fopen("somefile.txt", "r");
setvbuf(fp, lineBuf, _IOLBF, 1024); // set to line buffering
// ...
fclose(fp);
fp = fopen("another.dat", "rb");
setbuf(fp, NULL); // set to unbuffered
// ...
fclose(fp);
| << Prev | Beej's Guide to C | Next >> |