-
Understanding setvbuf()
Hello,
I am right now learning about file I/O operations using C through Prata's book. Right now I am on Chapter 13 of it.
I am unable to understand where setvbuf() can be used. I understand that we can control buffering with it but how can it be done? Can someone please give a small example of this function in action?
I have searched on the internet and I have not been able to find a clear example.
Regards,
Ishan
-
I am not sure if it is worth for a beginner to bother about.
But if you insist, there is an example: C library function - setvbuf() - Tutorialspoint
-
I have gone through that example earlier, but did not understand it.
-
Code:
/* compare these */
#include <stdio.h>
#include <unistd.h>
void no_buffering()
{
setvbuf(stdout, NULL, _IONBF, 0);
for (char c = 0x61; c < 0x7b; ++c)
{
putchar(c);
usleep(1e5);
}
}
void line_buffering()
{
setlinebuf(stdout); // default
for (char c = 0x61; c < 0x7b; ++c)
{
putchar(c);
usleep(1e5);
}
}
int main(void)
{
line_buffering();
no_buffering();
}