To fflush, or Not to fflush...

This is a discussion on To fflush, or Not to fflush... within the C Programming forums, part of the General Programming Boards category; I am going through the C for Dummies tutorials and have run into a problem using fflush. Code: #include <stdio.h> ...

  1. #1
    New Guy bjgough's Avatar
    Join Date
    Aug 2005
    Posts
    2

    To fflush, or Not to fflush...

    I am going through the C for Dummies tutorials and have run into a problem using fflush.

    Code:
    #include <stdio.h>
    
    int main()
    {
    	char c,d;
    	
    	printf("Enter a key, blah blah blah:\n");
    	c=getchar();
    
    	fflush(stdin);
    
    	printf("Enter a different key, blah blah blah:\n");
    	d=getchar();
    
    	if(c=='G' || c=='g' && d=='0')
    	{
    		printf("**** AUTO DESTRUCT ACTIVATED ****\n");
    		printf("****        GOODBYE        ****\n");
    	}
    
    	else
    	{
    		printf("Nice job moron.\n");
    	}
    	return (0);
    }
    A) When I use fflush, it compiles just fine, no errors.
    But when the program runs, and I type e I get the following result:

    Enter a key, blah blah blah:
    At the prompt I can type a letter...I choose 'e', then hit return and it spits out the next three lines, end program.
    e
    Enter a different key, blah blah blah:
    Nice job moron.

    B) When I use fpurge the compiler returns an error that reads:

    ld: Undefined symbols:
    ___gxx_personality_v0




    Sorry for the lengthy description, can anyone help me out?
    Bryan a.k.a. New Guy

  2. #2
    FOX
    Join Date
    May 2005
    Posts
    188
    If fflush(stdin) is really in the book, then the author is a dummy himself.

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Why fflush(stdin) is wrong. One would think that somebody would have told mr Gookin this by now. Perhaps he was not listening.

    I had to #include another header besides <stdio.h> in order to use fpurge. Maybe look into that. If you search the board you will likely find plenty of decent ways of clearing the input buffer.
    Last edited by kermit; 08-14-2005 at 05:29 PM.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    44
    fflush(stdin) is not entirely wrong, it's just not 'standard C'. Many compilers do support it, like MSVC. It all depends on which compiler you are using and whether you want your program to conform to 'standard C' or not.
    Last edited by n7yap; 08-14-2005 at 06:59 PM.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >fflush(stdin) is not entirely wrong
    If you want to use it in your own code, that's up to you. But around here, if the standard says it's undefined, it's wrong.

    >Many compilers do support it
    And many more do not. For example, since we're naming popular compilers, GCC doesn't support fflush ( stdin ).

    Let's just say that extensions should generally be avoided until one knows the standard language, and refrain from rationalizing constructs that are dangerous. You wouldn't want to encourage bad behavior just be cause "it works for you", would you?
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    44
    But around here, if the standard says it's undefined, it's wrong.
    I wasn't aware that was a rule on this board.

    If conforming to 'standard C' is a requirement, then yes, it is wrong.

    Just pointing out that some compilers do support it, and that is why it is seen in some code examples.

  7. #7
    New Guy bjgough's Avatar
    Join Date
    Aug 2005
    Posts
    2

    Question So...no fflushing, got it!

    Thank you for your response.
    The puzzler here is that fflush doesn't register an error when compiling, yet it doesn't work.
    fpurge ALSO recommended by Mr. Gookin (he does mention that fflush isn't the only way to do it) DOES return an error during compiling!?
    I have looked through the forum, and there seem to be really complex ways to clear the input buffer...

    I would like to know how to figure out what I AM supposed to use with the gcc compiler?

    My shell recognizes man fflush and fpurge as the same thing, they effect differently and neither of them works!?

    Definitely don't like being the new guy with the stoopid questions!
    Apologies,

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Rather than relying on flushing stdin, simply read all of the data in every time. Use somethign such as fgets to read an entire line in at a time and then process it. That way you don't leave anything on the buffer that shoudln't be there and you don't ahve to worry abotu fllushing. The reason ffllush'ing input buffers is undefined is because your process does not generally control an input buffer, something else does and you are reading it. So you don't have permissions to flush something you don't own. On top of that, fflush'ing something you read from does not really make sense. flushing does not mean 'throw away' data.

  9. #9
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    Quote Originally Posted by n7yap
    I wasn't aware that was a rule on this board.

    If conforming to 'standard C' is a requirement, then yes, it is wrong.

    Just pointing out that some compilers do support it, and that is why it is seen in some code examples.
    a book that tries to teach you about C should not include code like that.

    People who have no experience will automatically assume this is correct and start to use the construct in their own code. At some point they will have to unlearn it. When you switch to a compiler that doesnt support it for instance. Hey my code crashes suddenly and I have no idea what I'm doing wrong.
    Or when you start to work in a team where all the others start telling you you're a bad programmer because you dont even know that flushing stdin is undefined.

  10. #10
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Quote Originally Posted by orbitz
    Rather than relying on flushing stdin, simply read all of the data in every time. Use somethign such as fgets to read an entire line in at a time and then process it. That way you don't leave anything on the buffer that shoudln't be there and you don't ahve to worry abotu fllushing. The reason ffllush'ing input buffers is undefined is because your process does not generally control an input buffer, something else does and you are reading it. So you don't have permissions to flush something you don't own. On top of that, fflush'ing something you read from does not really make sense. flushing does not mean 'throw away' data.
    Although even when using fgets() there will be characters left in the buffer if the size of the storage is less than the amount of characters entered by the user.

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I wasn't aware that was a rule on this board.
    When you're trying to be a sarcastic prat, it's customary to use the smiley.

    >If conforming to 'standard C' is a requirement, then yes, it is wrong.
    Okay, try to understand it from our perspective. Most of the time the OP fails to tell us what compiler he is using, so 'standard C' is the only set of requirements we can go by. Even if he does mention his compiler, that doesn't change the fact that while any decent compiler will suitably conform to the standard, reliance on a feature that is clearly broken (even if it happens to work) is silly to the extreme.

    >Just pointing out that some compilers do support it
    Whether intentionally or not, you were encouraging it. I don't care if you encourage a non-portable feature; any non-trivial program has to use them, but I greatly disapprove of any non-portable feature that is hopelessly broken, yet fails to produce a diagnostic message in half of the compilers that use it.
    My best code is written with the delete key.

  12. #12
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    To fflush, or Not to fflush...
    If it's yellow, let it mellow.
    If it's brown, flush it down.

  13. #13
    Registered User
    Join Date
    Sep 2004
    Posts
    44
    Prelude,

    I get the impression you are a moderator here. If the scope of this board is programming in Standard C only, then it should be stated in the rules or guidelines. If that is the case, then I will abide by it, but I have read the rules and the guidelines and the stickeys, and I haven't seen that stated anywhere. It seems every time anyone, including me, mentions something that is not standard C on this board, they get flamed for it, often by you. So unless it is stated that the discussion here is to be standard C only, you need to widen your perspective and BACK OFF. I'm all for C standards, especially C99, but C programming does include using non-standard features of the compiler, calling API functions (whether Windows, Linux, Unix, Mac or whatever), inline assembly etc. If you limit yourself to standard C only, your programs are not going to be very interesting (or marketable).

  14. #14
    ---
    Join Date
    May 2004
    Posts
    1,379
    You call that a flame? You didn't do anything wrong, Prelude was just correcting you to what should have been said.

  15. #15
    Banned
    Join Date
    Jun 2005
    Posts
    594
    i agree, the point of this board is to help people, and to help
    people the best way, and standard code is the best way.
    i think you should welcome the information, and use it.
    prelude is hardly wrong when it comes to this board
    and programming in general, you should take her word
    for everything its worth i know i do, her post have taugh me
    much, and since im a a$$hole admitting that was hard, but
    worth the effort.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fflush and fgetc question
    By spank in forum Linux Programming
    Replies: 3
    Last Post: 05-24-2007, 05:39 AM
  2. When and where to use fflush (stdout)?
    By Micko in forum C Programming
    Replies: 8
    Last Post: 02-18-2005, 10:58 AM
  3. Problems with input and fflush
    By edugarcia in forum Linux Programming
    Replies: 1
    Last Post: 11-24-2004, 12:52 PM
  4. Why is fflush needed here?
    By Ariod in forum C Programming
    Replies: 2
    Last Post: 11-11-2004, 06:20 PM
  5. everyone's favorite...fflush
    By BungleSpice in forum C Programming
    Replies: 8
    Last Post: 03-12-2004, 10:53 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21