Thread: Newbie Question - fflush(stdin) & fpurge(stdin) on Mac and PC

  1. #16
    Registered User
    Join Date
    Feb 2006
    Location
    Sydney, Australia
    Posts
    40
    Quote Originally Posted by fgw_three
    Simply put, "a whitespace character in the control string causes whitespace characters to be read and discarded".
    Ok, I have a couple of questions on that...

    1) What is the control string? Is it the string entered by the user or something else?
    2) OK, so the whitespace characters (in this case the \n - I think - please tell me if I am wrong) are read by scanf()? ...And then discarded (does this mean ignored?)

    Regarding your teacher, I'll offer a "Personal Opinion Fueled by Personal Bias" here...

    Teachers are (for the most part in my experience) just that. They teach. In my experience, not that many of them have ever butted heads against code that needs to run on multiple platforms successfully, needing to be done on time, within budget etc etc. You learn a lot from living in that everyday programming environment, and teachers don't have that experience level.
    Yeah, I wasn't aware of it at first because this course I am doing has been my only contact with C ever but, since hanging out here and checking various web sources, it seems to me that my teacher is actually teaching some functions that are not standard C (eg. fflush(stdin)) but he is not aware of it. I'm not sure why he doesn't realise this (the fact that he only programs under Windows may have something to do with it) or, as you say, maybe he hasn't spent much time actually coding lately because he has been teaching. Whatever the reason, it has certainly caused me problems not to mention a metric buttload of stress.

    My next class is tomorrow so I will raise it with him then. I'm not sure how he will respond but I hope he takes it constructivly and not as a critcism. In my experience, teachers don't appreciate being corrected by their students so I don't hold very high hopes for a good outcome no matter how respectfully I raise the issue.

    If it turns out that he doesn't take it well, I may consider simply dropping the course and trying to study C on my own using the text books I have, web resources and advice from this forum. I hope it doesn't come to that though, there is really no substitute for the face to face tutoring you get with a proper teacher in a classroom.

    The other thing that concerns me is that a few people have implied in this thread that there are other things in his code that, while not actually wrong, are bad practice or non standard. For example...

    Is that code from the teacher? If you're teacher is teaching you fflush(stdin), then you should have that teacher reported to the school board. It's terrible code.
    and...

    Well if he comes out with "void main" and using gets() to read input, then you'll know for sure that you have a bad'un.

    These 3 things are the unholy trinity of the truly clueless.
    I must say that this is a bit worrying. I don't know enough to know what specifically is bad or why, but I am concerned that I may be being taught bad habits. This next one is particularly worrying...

    Your problem is down to your teacher teaching "works for me C" rather than standard C. fflush(stdin) does something apparently useful for most DOS programmers, but is utterly useless for anyone else - it may do something, nothing at all, or something completely hostile to your system.
    This is just the first example you have to deal with, god knows how many more horrors are in store. Best find a few online sources ( say here ) where you can verify everything your teacher says with people who have a clue.

    The whole flushing issue goes away if you use fgets() to read a line of input, then sscanf() (or any other string conversion function) to convert that input into a form you want.
    ...which seems to imply that I am being taught the incorrect (or at best non-standard or outdated) methods when other, better or more appropriate methods exist. I'm not sure if he will eventually cover the better methods or not but, even if he does, I have to wonder why we are being taught things that are frowned apon.

    Please visit here often, and ask your questions. You will usually get a good, solid answer. We were all noobs at one point or another, and I can't speak for everyone else, but I had good mentors along the way, so I feel I should return the favor.
    Yes, I will certainly do that. I have already gotten more solid information from this forum in less tha a week than I have gotten from my class in a full month and I am incredibly grateful for everyone's help. I was really starting to go under in class simply because I couldn't work out why the code (which I was assured was standard and cross platform) wasn't working on my Mac but was working for everyone else on PC.

    Thank you all for being so welcoming and supportive of nOObs such as myself. It is a phenomenon that does you credit as a group and it will certainly mean that I will be sticking around to ask and perhaps, one day even answer, questions.

    Good luck!
    Thanks! I think I need all the luck I can get at this stage.

  2. #17
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    ok the control string is this part:

    scanf (" %c", &foo);

    yes, the white space in the buffer is discarded - its not stored in
    your variable.

    and yes again, your teacher is teaching nonstandard code:

    void main should be int main
    gets should not be used - fgets is safer
    fflush (stdin) is designed to flush output streams - according
    to the standard, it has "undefined behavior" for input streams i.e.
    stdin.

    if the others crop up, get a copy of the standard and throw it at
    him!!!

    with regard to void main though, it will work on many compilers,
    but that doesn't make it right, the same goes for fflush (stdin).
    also, gets is perfectly legal, its just a bit crap!!! when you
    get to strings, try declaring an string 5 characters long,
    and using gets, enter 5 or more characters - you'll see why its
    hated!
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  3. #18
    Registered User 00Sven's Avatar
    Join Date
    Feb 2006
    Posts
    127
    Why is fflush(stdin); bad? I always use it in my programs to pause the program and it works every time. I use...
    Code:
    fflush(stdin);
    getchar();
    ...because otherwise getchar();does not pause it just gets the key press from the last thing that the user did. Is there a better way that I should be doing this? I used to use system(pause); but I was told that that was bad too. Is there any good way for me to pause?
    ~Sven

  4. #19
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by 00Sven
    Why is fflush(stdin); bad? I always use it in my programs to pause the program and it works every time.
    The FAQ is where frequently asked questions are answered already and available on demand.

    FAQ > Explanations of... > Why fflush(stdin) is wrong
    FAQ > How do I... (Level 2) > Flush the input buffer
    FAQ > How do I... (Level 1) > How do I get my program to wait for a keypress?
    Quote Originally Posted by 00Sven
    getchar();[/code]...because otherwise getchar();does not pause it just gets the key press from the last thing that the user did. Is there a better way that I should be doing this?
    Generally, take input in a way such that leftovers are not left over. Such as reading into a string and possibly converting to a numeric value or cleaning up the input string by removing an unwanted newline. There might even be an FAQ or two about that.

    FAQ > How do I... (Level 1) > Get a line of text from the user/keyboard (C)
    FAQ > How do I... (Level 1) > How do I get a number from the user (C)
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #20
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    read the FAQ to know why fflush bad. but here us an alternaive solution instead of fflush(stdin). this is small fucntion which clears the input buffer.

    Code:
    void clear_buffer(void)
    {
        int ch;
       
       while((ch=getchar)) != '\n' && ch != EOF);
    }
    call this fucntion instead of fflush(stdin) when ever u need to clear the input buffer

    ssharish2005

  6. #21
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Just as history repeats itself because nobody listens....

    > Why is fflush(stdin); bad?
    "works for me C" != standard C
    Which means sooner or later, you'll have to learn another solution.

  7. #22
    Registered User
    Join Date
    Feb 2006
    Location
    Sydney, Australia
    Posts
    40
    Right!

    Ok, I've done a fair bit of reading today and I've looked through the FAQs that Dave_Sinkula posted (thanks, btw) and I'm pretty sure I have a handle on this now. I at least understand it enough to be able to explain to my teacher why fflush(stdin) isn't a good idea and that's good enough for now.

    Thank you all for helping me to make sense out of this.

    Just one last question...

    When I originally came across the fflush(stdin) function and the problems it created, I found an alternative function (ie. fpurge(stdin)) that worked nicely when it was substituted for the offending fflush function.

    My question is, is fpurge defined for use with an input stream under the ANSI C Standard or is it just as dodgy as fflush and did I just get lucky when it fixed the problem?

    Actually, I have been trying (unsuccessfully as it turns out), to find some sort of master list that lists all the functions that you can and can't use when writing code to comply with the ANSI C standard. Does anything like this exist? If so, can anyone point me at it?

    Thanks.

  8. #23
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You have to pay $ for the actual C standards, but the draft documents are free to all to look at.
    C89-TC2 http://www.open-std.org/jtc1/sc22/wg...docs/n1124.pdf
    C99 http://www.open-std.org/JTC1/SC22/WG14/www/docs/n869/
    The difference between these and actual standards is probably only interesting to very experienced programmers and compiler writers.

    > My question is, is fpurge defined for use with an input stream under the ANSI C Standard
    It's not a standard function, therefore it's definition is entirely dependent on your implementation.

  9. #24
    Registered User
    Join Date
    Feb 2006
    Location
    Sydney, Australia
    Posts
    40
    Quote Originally Posted by Salem
    You have to pay $ for the actual C standards, but the draft documents are free to all to look at.
    C89-TC2 http://www.open-std.org/jtc1/sc22/wg...docs/n1124.pdf
    C99 http://www.open-std.org/JTC1/SC22/WG14/www/docs/n869/
    The difference between these and actual standards is probably only interesting to very experienced programmers and compiler writers.

    > My question is, is fpurge defined for use with an input stream under the ANSI C Standard
    It's not a standard function, therefore it's definition is entirely dependent on your implementation.
    Thanks for your help Salem, both in this post and in your previous posts. You are obviously extremely knowledgable in this field and, judging by the number of times your screen name pops up in other threads, you don't mind sharing some of that knowledge with those of us who don't know squat yet. I can't speak for everyone but I sure as brown, smelly stuff appreciate it.

    Cheers,
    TV

  10. #25
    Registered User
    Join Date
    Feb 2006
    Posts
    17
    Simply put, "a whitespace character in the control string causes whitespace characters to be read and discarded". That's from page 378 of "A C Reference Manual", fifth edition, by Harbison and Steele. (Very good book to have).
    And just to clarify things a little bit more, in C, newlines(\n), tabs(\t), return carriage(\r), etc are all refered to as whitespace.

    EDIT: forgot to close the "quote" tag
    Last edited by tcpl; 03-05-2006 at 07:35 AM.

  11. #26
    Registered User
    Join Date
    Feb 2006
    Location
    Sydney, Australia
    Posts
    40
    Just a quick update for all those who were/are following this thread...

    On Monday I approached my teacher regarding fflush(stdin) and mentioned (respectfully) that I have been advised that it may not be defined for input streams.

    He answered my query with... "Well, it's the way I have always done it and it has always worked.".

    His only concession being...

    "I think it used to not be defined, but it is now."

    Pathetic answer, IMHO.

    I then attempted to show him the FAQs that several very helpful people posted here that explain why using fflush(stdin) is not good practice. Namely this one .

    Unfortunately, he was totally uninterested in looking at the FAQ links that I had prepared. He glanced at my screen (which contained 5 or 6 fairly long paragraphs of text) for less than 2 seconds then nodded his head, said "Hmmm" and went on with what he was doing before.

    It was obvious to me that, not only did he did not read the screen, but he just didn't want to talk about it. As far as I can tell, he regards it as my problem and one in which he is not interested in discussing.

    So now, as you can probably imagine, I am really ........ed off. I can tolerate an incompetent teacher but not an apathetic one. My current opinion is that this teacher is a dud and I may be better off without his "help".

    There is a good chance that I will drop this class altogether and just learn C by myself using text books and online sources but I'm not sure how hard this will be. There is a lot to be said for face to face teaching (even if that teaching is sub-standard).

    I'd be interested to hear from anyone who has taught themselves C without any formal classes but I think I will start a new thread for that.

    Anyway, that's where I am at the moment.

  12. #27
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > "Well, it's the way I have always done it and it has always worked.".
    Yep, there's plenty of "works for me" types about.
    Probably explains why so many teachers and colleges stick like glue to ancient fossils like TurboC. They've probably downloaded things like dev-c++ in the past, but since none of their code works on the new compiler, they fail to see the real problem (that they don't in fact know C at all) and go back to their old compiler.

    > "I think it used to not be defined, but it is now."
    Obviously hasn't even read the standard.

    > My current opinion is that this teacher is a dud and I may be better off without his "help".
    Sounds like a good plan to me.


    > I'd be interested to hear from anyone who has taught themselves C
    Me for example

  13. #28
    Registered User 00Sven's Avatar
    Join Date
    Feb 2006
    Posts
    127
    I have started using the function that someone made earlier in this thread to clear standard input but I have found that my book specifically says that the way to do this is to use fflush(stdin);. So is it really that bad?
    ~Sven

  14. #29
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Yes, your book really is that bad!.

  15. #30
    Registered User 00Sven's Avatar
    Join Date
    Feb 2006
    Posts
    127
    lol That is not what I meant but I think that it might have been done for a reason. It uses this in almost the very beginning. I think that it assumes that readers would not understand what was wrong with it so he just used "works for me" C at the time and then explains it later.
    ~Sven

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A place to start - mac or pc?
    By GCat in forum C++ Programming
    Replies: 12
    Last Post: 11-19-2004, 12:21 PM
  2. Mac programming?
    By Luigi in forum A Brief History of Cprogramming.com
    Replies: 17
    Last Post: 02-07-2003, 09:37 PM