I'm running a program which starts a process and then that process writes to stdout which my program uses. Problem is, the output I need is about 42000 bytes. It seems the stdout buffer size is 8192 and I don't want it to flush until it gets to the 42000. Is there a way to set this?
I tried this:
setvbuf ( stdout , NULL , _IOFBF , 50000 ); // ie set it to 50000 bytes
on the code for the subprocess, but it doesn't seem to work at all. Does anyone have any ideas?
You need to supply a buffer to setvbuf() to work.
static char buf[50000]; /* buf must survive until stdout is closed */
setvbuf ( stdout , buf , _IOFBF , sizeof(buf) );
From the man page:
int setvbuf(FILE *stream, char *buf, int mode , size_t size);
...
Except for unbuffered files, thebufargument should point to a buffer at leastsizebytes long; this buffer will be used instead of the current buffer. If the argumentbufis NULL, only the mode is affected; a new buffer will be allocated on the next read or write operation.
Here is a sample program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (int argc, char *argv[]) {
char msg[42000];
char buf[50000];
setvbuf(stdout, buf, _IOFBF, sizeof(buf));
memset(msg, 'a', sizeof(msg));
msg[sizeof(msg)-1] = '\0';
puts(msg);
exit(0);
}
On my system, the strace output shows a single write of 42000 bytes.
As additional information to jxh answer:
char buf[50000];
setvbuf ( stdout , buf , _IOFBF , sizeof(buf) );
/*close stdout before the programs finishes or even before buf goes out of scope*/
fclose(stdout);
you'll have to close stdout aswell because man setvbuff also mentions:
You must make sure that the space that buf points to still exists by the time stream is closed, which also happens at program termination.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With