Thread: Problem with input stream

  1. #1
    VIM addict
    Join Date
    May 2009
    Location
    Chennai, India
    Posts
    43

    Problem with input stream

    Hi all,

    I have two doubts relating to the following code.

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int i;
        char c;
        printf("Enter some integer value:");
        scanf("%d",&i);
        __fpurge(stdin);
        printf("Enter some character:");
        scanf("%c",&c);
        printf("Input values are %d and %c\n",i,c);
        return 0;
    }
    1. In the above code if i remove the fpurge() function then i am not able to input the character (i.e.,) the scanf() for getting the character got skipped
    2. When i look into the man page for fpurge() it states that this function is not a standard one and also not portable, so i thought i can use fflush(stdin) but when i used fflush instead of fpurge the code works the same way i discussed in first step.

    Why is this behaviour happening?

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    fflush(stdin) has undefined behaviour.

  3. #3
    VIM addict
    Join Date
    May 2009
    Location
    Chennai, India
    Posts
    43
    So in this case, if i want to come up with a standard as well as portable code then what shall i use in the place of fpurge() function. Any clues?

  4. #4
    Registered User jimtuv's Avatar
    Join Date
    May 2010
    Location
    Sylvania, Ohio
    Posts
    94
    I am finding the FAQ has answers to questions like this

    How do I get a number from the user (C)

    It really is helping me out. Maybe it will help you too.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by cbalu View Post
    2. When i look into the man page for fpurge() it states that this function is not a standard one and also not portable, so i thought i can use fflush(stdin) but when i used fflush instead of fpurge the code works the same way i discussed in first step.
    fflush is meant for use on stdin, read the man page:

    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.
    stdin is an input stream, so you now have undefined behavior. Even if it seems to work don't do it! The same is probably true of fpurge, whoever wrote that code was being ignorant.

    If you want to learn how to deal with this issue properly read this:

    STDIN pitfalls

    It will take you about 10 minutes to achieve enlightenment
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    VIM addict
    Join Date
    May 2009
    Location
    Chennai, India
    Posts
    43
    MK27 thanks for your thoughts i will look at the link which you posted.

    whoever wrote that code was being ignorant
    Ofcourse it was non other than me. I was trying some other thing and landed with this problem so started searching for solution online and found out this was working.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by cbalu View Post
    Ofcourse it was non other than me. I was trying some other thing and landed with this problem so started searching for solution online and found out this was working.
    It's not your fault, this "ignorance" is propagated online, etc. But now you know!
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    VIM addict
    Join Date
    May 2009
    Location
    Chennai, India
    Posts
    43
    By the way, looks like the link which you pointed out was written by you, great explanation.

    Thanks for making it crystal clear.

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by cbalu View Post
    By the way, looks like the link which you pointed out was written by you, great explanation.

    Thanks for making it crystal clear.
    Yes, that's me This is probably the most common problem that comes up here, which is why I wrote that (thanks for the compliment, glad you liked it).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem collecting data from input file
    By gkoenig in forum C Programming
    Replies: 12
    Last Post: 03-30-2008, 07:40 AM
  2. reterning a reference to the input stream
    By indigo0086 in forum C++ Programming
    Replies: 3
    Last Post: 06-22-2006, 10:29 AM
  3. Problem getting the input from a temp variable into the Array
    By hello_moto in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2006, 01:50 AM
  4. Message printing problem
    By robert_sun in forum C Programming
    Replies: 1
    Last Post: 05-18-2004, 05:05 AM
  5. Problem with text input
    By newbie in forum C++ Programming
    Replies: 2
    Last Post: 03-10-2002, 04:44 PM

Tags for this Thread