Using setvbuf () with a STDIN stream

Im writing a small C program that should accept an input stream larger than 4096 bytes.

I found a post recommending using setvbuf () here:

Creating fgets for longer read () calls on Linux

It’s still very difficult for me to get this to work - this is some of my code that I am struggling with:

int main(void) 
{ 
#define MAX_STRING_SIZE 7168

char input_string[MAX_STRING_SIZE];

printf( "Input: " );

setvbuf( stdin, NULL, _IONBF, 0 );

fgets( input_string, MAX_STRING_SIZE-1, stdin );

printf( "\n" );
printf( "%s", input_string );
} 

Has anyone managed to increase this input buffer?

My environment: Ubuntu 10.10 with build-essential

Thank!

+3
source share
2 answers

_IONBF, . _IOFBF , , ( ). , , :

setvbuf(stdin, NULL, _IOFBF, 16384);

. :

char mybuffer[32768];

setvbuf(stdin, mybuffer, _IOFBF, sizeof(mybuffer));
+4

. - , fgets (, , fgetc fscanf), , read syscalls , 1 . , , ( fread), , , . , setvbuf , , , , -.

+3

All Articles