Find the answer to your Linux question:
Results 1 to 8 of 8
I have a problem with fflush(stdin) function in C, that when I am taking input from the user and through characterd variable from user it don't work like.. Code: printf("No.:"); ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jul 2005
    Posts
    17

    problem with fflush(stdin) function


    I have a problem with fflush(stdin) function in C, that when I am taking input from the user and through characterd variable from user it don't work like..
    Code:
    printf("No.:");
    scanf("%d",&a);
    printf("Character");
    scanf("%c",&ch);
    is not working. Please help me.

  2. #2
    Linux Guru lakerdonald's Avatar
    Join Date
    Jun 2004
    Location
    St. Petersburg, FL
    Posts
    5,035
    You really should use fgets() combined with sscanf(), as scanf() messes up a lot when it comes to reading newlines, etc.

  3. #3
    Linux Engineer Giro's Avatar
    Join Date
    Jul 2003
    Location
    England
    Posts
    1,219
    I havent touched C for a while but isnt fscanf("%s", &myBuff, stdin); the best thing to use for user intput? And should'nt you use getline() to get a line of input instead of fgets().

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Guru lakerdonald's Avatar
    Join Date
    Jun 2004
    Location
    St. Petersburg, FL
    Posts
    5,035
    Quote Originally Posted by Giro
    I havent touched C for a while but isnt fscanf("%s", &myBuff, stdin); the best thing to use for user intput? And should'nt you use getline() to get a line of input instead of fgets().
    The scanf() family of functions are notoriously bad for handling user input, so you read it into a buffer with fgets() and then do the conversion with sscanf(). getline() is part of the Readline library, and is considered generally more powerful than the fgets()-like line reading routines. However, if you are not going to use tab-completion or anything fancy, there's no point in really using the Readline library.

  6. #5
    Linux Newbie
    Join Date
    Jul 2005
    Location
    Chd,India
    Posts
    135
    now that the problem is sorted out,guys could u tell me something about input buffering
    i understand that commands like fgets and fputs have their stdio buffers
    and read and write are system calls
    but still,shoulnt output be the only one that causes buffering problems

    i mean the whole idea of input buffering causing problems is vague(and new ) to me
    The strong shall live and the weak will die
    In the end,only the fittest survive in this world
    - Shishio Makoto

  7. #6
    Linux Guru lakerdonald's Avatar
    Join Date
    Jun 2004
    Location
    St. Petersburg, FL
    Posts
    5,035
    As I understand it, if you did not buffer input, you would be forced to grab each character as it comes, and manipulate it directly, instead of waiting for a newline, and taking that input as a whole (sometimes the former is desirable, however.)

  8. #7
    Linux Newbie
    Join Date
    Oct 2004
    Posts
    158
    FWIW -

    fflush(stdin) does not have defined behavior, according to the C standard, which gcc supports. So, you can't use it to clear an input buffer. Only output.

    If you want to read a file, you cannot have a zero-length buffer. To change buffer size use setvbuf() for files that are open with FILE * descriptors. Performance goes down the tube wih really small buffers.

    Buffering is required in both directions - input & output.

    stdin doesn't have to be the keyboard - it could be a pipe.

    Decent unix programs read either from stdin or from a pipe into stdin with no problem.

    A good rule of thumb is to leave stdin and stdout alone, except for special applications. If you want to read keystrokes try changing tty properties to turn off canconical mode like this rather than messing up stdin:

    Code:
    #include <termios.h>
    #include <unistd.h>
    #include <assert.h>
    #include <string.h>
    /*------------------------------------------------*/
    int getch&#40;void&#41; &#123;
          int c=0;
    
          struct termios org_opts, new_opts;
          int res=0;
              //-----  store old settings -----------
          res=tcgetattr&#40;STDIN_FILENO, &org_opts&#41;;
          assert&#40;res==0&#41;;
              //---- set new terminal parms --------
          memcpy&#40;&new_opts, &org_opts, sizeof&#40;new_opts&#41;&#41;;
          new_opts.c_lflag &= ~&#40;ICANON | ECHO | ECHOE | ECHOK | ECHONL | ECHOPRT | ECHOKE | ICRNL&#41;;
          tcsetattr&#40;STDIN_FILENO, TCSANOW, &new_opts&#41;;
          c=getchar&#40;&#41;;
              //------  restore old settings ---------
          res=tcsetattr&#40;STDIN_FILENO, TCSANOW, &org_opts&#41;;
          assert&#40;res==0&#41;;
          return&#40;c&#41;;
    &#125;

  9. #8
    Just Joined!
    Join Date
    Dec 2005
    Location
    Assam, India
    Posts
    36
    Is there any way to flush the input buffer? The problem where I faced it will make it clear what I want exactly.

    Code:
    main(){
    
    	char buffer[20];
    	
    	scanf(“%s”, buffer);
    	getchar();
    
    }
    Since the scanf does not take in the newline when the enter key is pressed, it remains in the buffer which is taken by getchar(); thus it does not wait for user input anymore. Of course this problem can be solved by replacing scanf with functions such as gets() or similar function as follows:-

    Code:
    main(){
    
    	char buffer[20];
    	
    	gets(buffer);
    	getchar();
    
    }
    But I want to know if there is any way to flush the input buffer. fflush () works for output stream not input; aren’t there any equivalent function for input stream?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •