operating system - What does fflush(stdin) do in C programing? -


this question has answer here:

i new in c programing , trying understand how fflush(stdin) works.

in following example fflush(stdin) clears buffer or clears ever entered after third item? mean user enters account number , space, name , space , balance .is true point on, ever user enters flushed fflush(stdin) ? , stdin won't empty.

why because enters while loop , start writing text file.

my second question whether crtl-z tell os stop asking user entering input?

printf( "enter account name , balance. (separated spaces)\n" );   printf( "enter eof end input. (crtl-z)\n" );   printf( "? " );   scanf( "%d%s%lf", &account, name, &balance );   fflush(stdin);    // write account, name , balance file fprintf   while ( !feof( stdin ) )   {       //fflush(stdin);      fprintf( cfptr, "%d %s %.2f\n", account, name, balance );      printf( "? " );      scanf( "%d%s%lf", &account, name, &balance );   }    fclose( cfptr ); 

the answer fflush(stream) formally defined output streams, fflush(stdout) ok, fflush(stdin) not.

the purpose of fflush(stream) make operating system flush buffers underlying file. example of legitimate use, students have problems “my prompt doesn't appear!” if like:

printf("enter number: "); 

however, find works fine:

printf("enter number:\n"); 

of course, don't want newline after prompt, have bit of problem.

the reason output stdout buffered os , default behavior (often) write output terminal when newline encountered. adding fflush(stdout) after printf() solves problem:

printf("enter number: "); fflush(stdout); 

now, working analogy, people think fflush(stdin) should discard unused input, if think little bit doesn't make sense. mean “flush” input buffer? “flushed” to? if flush output buffer, output sent underlying file or terminal, wind anyway, input “eventually end anyway”? there's no way of knowing! should behavior if input stream data comes file or pipe or socket? isn't at all clear input streams behavior of fflush() should be, it's clear output streams in cases. hence, fflush() defined output streams.

the reason why erroneous use of fflush(stdin) became commonplace that, many years ago, few operating systems did implement scheme worked many people expected, discarding unused input. microsoft dos example. surprisingly, modern versions of linux implement fflush() input streams.

the right thing “extra” unwanted terminal input read , nothing it. easy calling fflush(stdin), works everywhere, , doesn't rely on formally undefined behavior.

the c standard says:

if stream points output stream or update stream in recent operation not input, fflush function causes unwritten data stream delivered host environment written file; otherwise, behavior undefined.

posix says (also explicitly defers c standard):

if stream points output stream or update stream in recent operation not input, fflush() shall cause unwritten data stream written file, ...

but linux manpage says:

for output streams, fflush() forces write of user-space buffered data given output or update stream via stream's underlying write function. for input streams, fflush() discards buffered data has been fetched underlying file, has not been consumed application. open status of stream unaffected.


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -