Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Abominus

stupid fflush() problem.

This topic is 6021 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

why does fflush behave differently on Linux than a Win32 console? eg. fgets( buffer, sizeof(buffer)/sizeof(char), stdin ); if ( !strchr( buffer, "\n" ) ) fflush( stdin ); on linux, fflush() doesn''t have the desired effect, and the input spills over to the next fgets() call. Why?

Share this post


Link to post
Share on other sites
Advertisement
The ANSI standard doesn''t specify the behaviour of flushing an input stream as far as I know, so the behaviour is undefined. This should work:

fgets(buffer, sizeof(buffer), stdin);
if(!strchr(buffer, ''\n'')) while(fgetc(stdin) != ''\n'');

BTW: sizeof(char) is always 1, since sizeof returns the size of the data type measured in chars, not bytes. You should also use ''\n'' instead of "\n". I fixed both of these in my example.

Share this post


Link to post
Share on other sites
quote:
Original post by Null and Void
The ANSI standard doesn''t specify the behaviour of flushing an input stream as far as I know, so the behaviour is undefined. This should work:

fgets(buffer, sizeof(buffer), stdin);
if(!strchr(buffer, ''\n'')) while(fgetc(stdin) != ''\n'');

BTW: sizeof(char) is always 1, since sizeof returns the size of the data type measured in chars, not bytes. You should also use ''\n'' instead of "\n". I fixed both of these in my example.




Thanks. But are you sure about the second bit? sizeof returns the size of the data type in bytes. And sizeof char is not always 1, especially on Unicode/MCBS capable compilers.

Share this post


Link to post
Share on other sites
quote:
Original post by Abominus
...sizeof char is not always 1, especially on Unicode/MCBS capable compilers.

The standard specifies (IIRC) that sizeof(char) = 1 <= sizeof(short int) <= sizeof(long int) <= sizeof(float) <= sizeof(double) <= sizeof(long double)

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ ]
[ MS RTFM [MSDN] | SGI STL Docs | Boost ]
[ Google! | Asking Smart Questions | Jargon File ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites
quote:
Original post by Abominus
sizeof returns the size of the data type in bytes.

quote:
Page 81, C99 Draft
6.5.3.4 The sizeof operator
...
3 When applied to an operand that has type char, unsigned char, signed char, (or a qualified version thereof) the result is one. ...
...



Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!