flushall()

This is a discussion on flushall() within the C Programming forums, part of the General Programming Boards category; Even spelled with one f? Hey Mat, sorry my bad with 2 'f', ya but still not available, i fact ...

  1. #16
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Even spelled with one f?
    Hey Mat, sorry my bad with 2 'f', ya but still not available, i fact i had never heard of that function before and only function which i had come across was fflush which is undefined for stdin as you have been explaining. But ya still the same

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

  2. #17
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by tabstop View Post
    Well, what's wrong with it?

    If you're worried about making sure that the prompt gets out there before the input gets read in, you can fflush(stdout) before the scanf. Most compilers know that stdin and stdout are interactive, so that everything gets done properly; but if you don't trust them, you can force the issue.
    According to the standard, stdout is line buffered by default when going to a terminal. This means it will not flush unless you print a newline, or explicitly call fflush(). You should never assume that stdout will flush when doing interactive input.

    As for flushall(), I've never heard of that function. Assuming it flushes all open files, it would be a total waste to call it just for this. Don't worry about flushall() -- just call fflush(stdout).

  3. #18
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    The OP should really understand of why fflushall or fflush shouldn't be used to clear the input buffer. And thats what everyone i trying to say. The FAQ should have give you some anlternative solution of how to clear the input buffer rather than using those function which is more specific to clearing the input buffer in certain cases unless need.

    Did you see this small function in one of our FAQ
    Code:
    void clear_buffer( void )
    {
         int ch;
         
         while( ( ch = getchar() ) != '\n' && ch != EOF );
    }
    If you still wanted to use those scanf function then you will have to call this function in the right place to clear the input buffer. But my suggestion is not to use scanf family function or perhaps other members suggestion as well.

    Being said to use the above function, if you call this function unnecessarily, when the input buffer is clear, the execution might come to halt till it finds an '\n' in the input buffer. So you should be more carefully while calling them.

    ssharish

  4. #19
    Registered User
    Join Date
    Dec 2007
    Posts
    84
    Quote Originally Posted by matsp View Post
    Integers have finite capacity - about 2 billion on a 32/64-bit system, 32000 and a bit on a 16-bit compiler.

    If you want to deal with larger numbers than that, you need to use a different type than int - what is the right solution depends on what you actually are trying to do.

    --
    Mats
    Sorry I don't think I've explained the problem well.
    The problem is not about capacity (it's a different issue).
    It append when you put a string longer than the array size, in this case fgets() will not be called! Bringing me back to scratch.
    So using fgets + sscanf with input longer than the array equel to:
    Code:
    scanf("%d",&num);
    gets(arr);
    In both of them ONLY a number will be entered.

    Here is the code I've used:
    Code:
    int main(void)
    {
      char cNum[10];
      int iNum=0;
      char cStr[20];
     
      puts("Enter a number:");
      fgets(cNum,10,stdin);
      sscanf(cNum,"%d",&iNum);
      puts("Enter a string:");
      fgets(cStr,20,stdin);
      
      printf("Num=%d\n",iNum);
      puts(cStr);
      return 0;
    }

    Thanks again!

  5. #20
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Let me explain what's happening here.

    When you ask scanf() to read a number, it will discard any spaces it finds, and then read in as many digits as it finds, and then return. (Assuming no errors occur.) This leaves any spaces and newlines (whitespace) after the number still in the input stream. If you want to read the next line, you have to discard any spaces on the current line first.

    You're doing this with fgets() and gets(), but since you don't really care what you're discarding, you can just read one character at a time until you get a newline.
    Code:
    while(getchar() != '\n');
    That's assuming I've understood your problem correctly.

    Or maybe you're stumbling across the fact that fgets() will not read an entire line if there are more characters in the line than will fit in the buffer you passed it. If you're worried about this, you could examine the last character in the string fgets() read -- if it's not a newline, and EOF was not encountered (check fgets()'s return value), then the whole line couldn't fit into your buffer.

    I suggest you read this thread, especially my responses. I don't want to write a code I don't understand ..!!
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #21
    Registered User
    Join Date
    Dec 2007
    Posts
    84
    Hi Guys,
    I wasn't expecting so many replys on this one:
    I will post my question again to focus on my problem.

    I am looking for an efficient way to flush stdin (IN C) to avoid the following known problem:

    Code:
    scanf("%d",&num);
    fgets(buf,80,stdin);
    scanf() will take the number and leave newline in the stream so fgets() will not wait for the user input.
    "The function fflush forces a write of all user-space buffered data for the given OUTPUT or update stream via the stream's underlying write function. The open status of the stream is unaffected. If the stream argument is NULL, fflush flushes all open output streams."
    The behaviour of fflush() for stdin is undefined! and should not be used for input.
    So this one is not an option.

    The second option I could use is:
    Code:
    while ((c = getchar()) != '\n' && c != EOF);
    This will clear the buffer, but if there is no data in the input stream, we will have to wait until there is, which is again undesirable, so this is not an option too.

    My 3rd option was not to mix fgets and scanf, so I came up with the following code:
    Code:
    int main(void)
    {
      char cNum[10];
      int iNum=0;
      char cStr[20];
     
      puts("Enter a number:");
      fgets(cNum,10,stdin);
      sscanf(cNum,"%d",&iNum);
      puts("Enter a string:");
      fgets(cStr,20,stdin);
      
      printf("Num=%d\n",iNum);
      puts(cStr);
      return 0;
    }
    Everything works fine until the size of the input is bigger than the size of the array. In this case the results are the same as:
    Code:
    scanf("%d",&num);
    fgets(buf,80,stdin);
    So this one is not an option eigher.
    There must be an ANSI solution for this, it is impossible that C ANSI don't give this basic option and forcing me to hack the most basic operation of user input.
    Please guys guide me here, I am running out of options and it is driving me CRAZY!

    Thanks a million
    Salvador
    Last edited by salvadoravi; 12-24-2007 at 11:58 AM.

  7. #22
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by salvadoravi View Post
    Hi Guys,
    I wasn't expecting so many replys on this one:
    I will post my question again to focus on my problem.

    I am looking for an efficient way to flush stdin (IN C) to avoid the following known problem:

    Code:
    scanf("%d",&num);
    fgets(buf,80,stdin);
    scanf() will take the number and leave newline in the stream so fgets() will not wait for the user input.
    "The function fflush forces a write of all user-space buffered data for the given OUTPUT or update stream via the stream's underlying write function. The open status of the stream is unaffected. If the stream argument is NULL, fflush flushes all open output streams."
    The behaviour of fflush() for stdin is undefined! and should not be used for input.
    So this one is not an option.
    That is correct - fflush is not an option of input files.

    The second option I could use is:
    Code:
    while ((c = getchar()) != '\n' && c != EOF);
    This will clear the buffer, but if there is no data in the input stream, we will have to wait until there is, which is again undesirable, so this is not an option too.
    But as I stated, if you use this in conjunction with scanf, it's fine - because scanf _IS_ guaranteed to leave at least a newline ('\n') in the input stream. So there will be data in the input stream after scanf().

    My 3rd option was not to mix fgets and scanf, so I came up with the following code:
    Code:
    int main(void)
    {
      char cNum[10];
      int iNum=0;
      char cStr[20];
     
      puts("Enter a number:");
      fgets(cNum,10,stdin);
      sscanf(cNum,"%d",&iNum);
      puts("Enter a string:");
      fgets(cStr,20,stdin);
      
      printf("Num=%d\n",iNum);
      puts(cStr);
      return 0;
    }
    Everything works fine until the size of the input is bigger than the size of the array. In this case the results are the same as:
    Code:
    scanf("%d",&num);
    fgets(buf,80,stdin);
    So this one is not an option eigher.
    There must be an ANSI solution for this, it is impossible that C ANSI don't give this basic option and forcing me to hack the most basic operation of user input.
    Please guys guide me here, I am running out of options and it is driving me CRAZY!

    Thanks a million
    Salvador
    Well, if you use fgets() to read a number, and you can't rely on the user to enter a "short" number, then you may want to extend the size of the buffer. In a modern machine, a 200 byte buffer is "a drop in the ocean", with regards to memory usage. So if you make your input buffer "long enough that anyone except the most malicious user" will never fill the buffer, then you should be fine.

    As dwks points out, you CAN check if the input was terminated by "out of buffer or newline" - by the fact that the input buffer contains a newline if it got there before the buffer was filled. If you don't get a newline, you will again need to read until a newline, so the solution is the same. But if there is no newline in the input buffer [and we didn't hit end-of-file conditions] there is a newline somewhere else in the input stream - that is guaranteed, because there is only two conditions that the data is made available to the actual application:
    1. The input was terminated by a newline.
    2. The input was terminated by end-of-file.

    The input is held by the OS until one of the two above conditions apply [unless you go messing with very specific settings in the input system of your OS - and you would know if this is happening].

    --
    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.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 19
    Last Post: 04-04-2009, 09: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, 10: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

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