Thread: flushall()

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Quote Originally Posted by Abda92 View Post
    why is flushall() included in the standard library if it has undefined behavior?
    I dont even have fflushall() on my compiler

    Code:
      [Linker error] undefined reference to `fflushall'
    ssharish

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by ssharish2005 View Post
    I dont even have fflushall() on my compiler

    Code:
      [Linker error] undefined reference to `fflushall'
    ssharish
    Even spelled with one f?

    But yes, it's not standard.

    One would expect that the function would safe-ish - but to assume that fflush() on an input actually does what the above code expects is a different story.

    And assuming that it does, why would anyone think that it's better to call flushall() than [not that I'm recommending this solution either] fflush(stdin)? The only difference is that flushall() _ALSO_ flushes any other files that may be open at this time - what's the benefit of that? Note that flushing a file _HAS_ side-effects on that file - so unless you know exactly all the files are open and in what state they are, you are "messing with things you don't have a right to mess with".

    Also to the original poster about "how do I know I can call the clear input buffer code?" - if you have successfully come back from a scanf() call, then you DO know that there is at least a neline character ('\n') somewhere waiting to be read. Whether there is also "other stuff" in there is a different question. But there will certainly be a newline - there is no doubt about that. That is because scanf will not receive any of the data from the console/input file before a newline is encountered [1]. This is guaranteed by the way that stdio functions work in C [2].


    [1] With one exception - if an end-of-file is reached before a newline is seen, then the input buffer is also released to the input function - so make sure you check for end-of-file when clearing the input buffer.

    [2] In some systems YOU CAN change the behaviour of the console input buffering - but this is special functions that you wouldn't use in normal programming. Of the many, many programs I've written so far, maybe 1-2% of them have used this, and I'm sure there are programmers who have worked 20 years without EVER using this sort of modified behaviour of IO - and you can't [in a reliable way] use scanf() or fgets() in this mode of operation.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 19
    Last Post: 04-04-2009, 08:37 AM
  2. flushall() in DEVC++
    By vddking2 in forum C++ Programming
    Replies: 1
    Last Post: 11-21-2006, 09:26 PM
  3. clearing invalid value from memory
    By StringQuartet13 in forum C++ Programming
    Replies: 3
    Last Post: 09-29-2003, 09:19 PM
  4. Serial Communications with win32 api
    By Blackthorne in forum C Programming
    Replies: 1
    Last Post: 01-26-2003, 12:45 PM
  5. what is flushall() ?
    By drharv in forum C Programming
    Replies: 3
    Last Post: 02-18-2002, 10:13 PM