Thread: fflush(stdin)

  1. #1
    Unregistered
    Guest

    fflush(stdin)

    Someone says that this isn't defined. Is it possible that it works, even though it isn't defined?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Undefined behavior can do anything. It may work for you, but that doesn't mean you should use it.

    -Prelude
    My best code is written with the delete key.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Someone says that this isn't defined
    Everyone - especially those who wrote the ANSI-C standard

    > Is it possible that it works
    Undefined things never work. Part of 'could do absolutely anything' (which is what undefined means) includes doing what you expect/want, but that shouldn't be mistaken for working.

    All manner of things could happen which could result in your program suddenly doing something different.

  4. #4
    Unregistered
    Guest
    Prelude,
    I'm in the middle of reading C in 21 Days (1997 edition), and it uses fflush(stdin) all over the place, as well as void main, and global variables. I'm shocked!

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I'm shocked!
    Don't be, that book is widely accepted as a foot rest.

    -Prelude
    My best code is written with the delete key.

  6. #6
    Unregistered
    Guest
    Prelude,
    Oh well, it only cost me 20 bucks used. Although C in 21 Days has a lot more errors that a book like Deitel, for example, it's easier to understand for a fairly new guy like me. Deitels can't relate to dumb guys like me. By the way, did you say in another thread that you didn't like being called he...

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >did you say in another thread that you didn't like being called he...
    It really doesn't bother me, I've long since gotten used to people making assumptions about me. But every once and a while it's fun making people uncomfortable by telling them that I'm not a guy.

    -Prelude
    My best code is written with the delete key.

  8. #8
    Unregistered
    Guest
    Prelude,
    This forum is almost 100% guys. It's hard to tell from your picture, but, you sound like a guy, that's why the comment caught my attention. By the way, at work do you keep several C books around to refer to, if necessary? Do you know C++ and Java?

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >you sound like a guy, that's why the comment caught my attention
    Really? This of course begs the question, what does a guy sound like?

    >at work do you keep several C books around to refer to, if necessary?
    Most definitely. As much as I would love to have photographic memory, I'm stuck looking up things that I'm not sure about.

    >Do you know C++ and Java?
    Yes.

    -Prelude
    My best code is written with the delete key.

  10. #10
    Unregistered
    Guest
    You sound like a guy because you don't sound touchy-feely. I thought all the whiz-kids were into Java these days? Gotta go rollerblading...

  11. #11
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    This snippet is from the Visual C compilers online help regarding fflush().. I can see why people have a false sense of security when using this method as the documentation says its ok.
    Code:
    /* FFLUSH.C */
    
    #include <stdio.h>
    #include <conio.h>
    
    void main( void )
    {
       int integer;
       char string[81];
    
       /* Read each word as a string. */
       printf( "Enter a sentence of four words with scanf: " );
       for( integer = 0; integer < 4; integer++ )
       {
          scanf( "%s", string );
          printf( "%s\n", string );
       }
    
       /* You must flush the input buffer before using gets. */
       fflush( stdin );
       printf( "Enter the same sentence with gets: " );
       gets( string );
       printf( "%s\n", string );
    }
    But I would like to ask your opinion Prelude, if you do have data in you input buffer - what would you suggest to do? I had this problem in a project that I handed in recently and decided to use the fflush(stdin) method, knowing it was wrong, but also knowing that my tutors where inexperienced enough to not pick up on it, (their text books still use void main()!!). But I hate hacking code together like that. Any suggestions? The problem I had was that no matter what function I used to read the users input, even where it explicitly read only a certain number of characters, whatever was left over would reside in the stdin buffer until the next time the user was asked for input...

    If this does not make sense I can post some code

    Cheers
    Last edited by foniks munkee; 04-20-2002 at 11:56 PM.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > void main( void )
    > fflush( stdin );
    > gets( string );
    Could this example be any worse?

    > But I hate hacking code together like that
    The simple way out of this mess is to always use fgets to read the input. It will read input up to the next newline or until the buffer is full.

    And it has a simple interface to tell you whether you have reached the end of file, or there's been an i/o error (it returns NULL).

    Once you have the input in an array, a wide variety of approaches are possible, including sscanf, strncmp, strtok, a for loop, etc

  13. #13
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    Yeah, that is what I thought - I assumed (incorrectly) that the following code would work. But anything that is greater than the 10 bytes that I want fgets() to read is stored in the stdin buffer until the next call to fgets() where it is then stored in the second array, buffer2. Have I incorrectly assumed that fgets() would discard everything that is not required, or do I have to do some cleaning up before the next call the fgets()?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
    	char buffer1[80];
    	char buffer2[80];
    	
    	printf("Enter a string: ");
    	fgets(buffer1, 10, stdin);
    	printf("Enter another string: ");
    	fgets(buffer2, 10, stdin);
    
    	printf("\nstring1 : %s", buffer1);
    	printf("\nstring2 : %s", buffer2);
    
    	return 0;
    }

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    fgets doesn't throw away any excess input, it's there for the next call to fgets.

    printf("Enter a string: ");
    fgets(buffer1, 10, stdin);

    What you would then do (to see if you had any excess input), would be this
    if ( strchr( buffer1, '\n' ) == NULL ) {
    // there is no newline in this buffer, must have been a long line
    // throw away the excess
    }

    But reading directly into the final result is perhaps not a good idea, normally you want to validate the input in some way, or perhaps remove the newline which fgets appends.

    char buff[BUFSIZ];
    fgets( buff, BUFSIZ, stdin );
    // now move 10 chars from buff to buffer1, if your tests on buff show that this is a good (and safe) thing to do

    BUFSIZ is a macro value in stdio.h

  15. #15
    Registered User foniks munkee's Avatar
    Join Date
    Nov 2001
    Posts
    343
    Ahhh, that is good advice - thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. fread/fwrite
    By chopficaro in forum C Programming
    Replies: 6
    Last Post: 05-11-2008, 01:48 AM
  3. Searching Into Files
    By St0rM-MaN in forum C Programming
    Replies: 12
    Last Post: 04-26-2007, 09:02 AM
  4. Newbie Question - fflush(stdin) & fpurge(stdin) on Mac and PC
    By tvsinesperanto in forum C Programming
    Replies: 34
    Last Post: 03-11-2006, 12:13 PM
  5. Linked List Need Help Urgent
    By ykchua in forum C Programming
    Replies: 5
    Last Post: 08-17-2004, 02:57 PM