Thread: fgets and scanf

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    32

    fgets and scanf

    Hello,

    I'm aware that scanf is a bit "nasty" because it often leaves "undigested" characters in the input stream. What about fgets used to input a string from stdin? I tried entering more characters than I had specified in fgets and nothing seemed to remain in the input stream. Is my observation correct?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    No .

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by kkk View Post
    ....
    Is my observation correct?
    I am not sure about the general trend I am seeing here(not with you), but I would like to point out although it is Computer Science it is not science as in trying to figure out how the universe works. You are not exploring like Capt. Kirk here.....
    LOOK AT THE DOCUMENTATION. It tells you what a function does and how it will behave. You do not need to observe and infer here.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by AndrewHunter View Post
    I am not sure about the general trend I am seeing here(not with you), but I would like to point out although it is Computer Science it is not science as in trying to figure out how the universe works. You are not exploring like Capt. Kirk here.....
    LOOK AT THE DOCUMENTATION. It tells you what a function does and how it will behave. You do not need to observe and infer here.
    And I suppose the corollary to that is: if you are going to write a function to test out the behavior of a function, you should be careful to write your program to actually test the behavior of the function (which apparently didn't happen here).

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    32
    Quote Originally Posted by AndrewHunter View Post
    LOOK AT THE DOCUMENTATION. It tells you what a function does and how it will behave. You do not need to observe and infer here.
    I did consult the man pages and I did try to look it up on the internet. There was nothing about what fgets does with the characters it doesn't "put" in the string.

    That's why I asked. Sorry if it bothered you.

    Caroline

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by kkk View Post
    I did consult the man pages and I did try to look it up on the internet. There was nothing about what fgets does with the characters it doesn't "put" in the string.
    There aren't any such characters, which may be why the man page is silent about it. (Or at any rate, there aren't any characters that fgets processes but doesn't put in the string.) Look:
    Quote Originally Posted by man fgets
    The fgets() function reads at most one less than the number of characters
    specified by n from the given stream and stores them in the string s.
    It does what it says on the tin: it reads no more characters than can fit. The remaining characters must needs be still unread. (ETA: If you want an explanation of why your code is doing whatever it is that it's doing that leads you to believe that fgets discards characters, you'd have to provide the code.)

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    How did you come by this observation? To become good at programming, you should be able to test such ideas with practice snippets of code. For example, I figured out the answer to your question with this:

    Code:
    // quick and dirty example
    
    #include <stdio.h>
    
    #define MAX_LEN 10
    
    int main(void)
    {
        char str_input[MAX_LEN];
        int left_over = 0;
    
        // read user input, 9 characters max
        printf("Enter string:  ");
        fgets(str_input,MAX_LEN,stdin);
        // quick and sloppy null termination at the end of the character array
        str_input[MAX_LEN-1] = '\0';
    
        // at runtime, we will enter 15 characters, followed by enter (newline)
    
        // now we'll print our string:
        printf("\nString:  %s\n\n",str_input);
    
        // ... and print out anything left over in the input buffer, up to a newline
        printf("Left over characters:  ");
        while((left_over = getchar()) != '\n')
            printf("%c",left_over);
    
        printf("\n\n");
        return 0;
    }
    And here is my output:

    Code:
    Enter string:  ABCDEFGHIJLKMNO
    
    String:  ABCDEFGHI
    
    Left over characters:  JLKMNO
    Therefore, we see that when the data in the input buffer exceeds the limit of "fgets()", the rest of the data continues to sit in the buffer.
    Last edited by Matticus; 07-29-2011 at 09:34 AM.

  8. #8
    Registered User
    Join Date
    May 2011
    Posts
    32
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAXSIZE 5
    
    char numbers[MAXSIZE + 1];
    
    int myatoi(const char *str)
    {
        int val = 0;
        
        for (int i = 0; str[i] >= '0' &&  str[i] <= '9'; ++i) {
            val = val * 10 + (str[i] - '0');
        }
        return val;
    }
    
    void get_strnr2()
    {
        fgets(numbers, MAXSIZE, stdin);
    }
    
    
    int main (int argc, const char * argv[])
    {
        
        int result, c;
       
        printf("\nEnter a number:");
        get_strnr2();
        result = myatoi(numbers);
        printf("\nYou entered: %i", result);
        
        while ((c = getchar()) != '\n') {
            putchar(c);
        }
        return 0;
    }
    Try to test the code above and enter a number containing more than 5 digits. The leftover characters don't get printed... Why? I'm running Mac OS X Lion and Xcode 4.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Because they are. Try changing this erroneous line:
    Code:
    printf("\nYou entered: %i", result);
    to something more correct, like
    Code:
    printf("\nYou entered: %i>>result stops here", result);

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    What is your output when you enter excessive digits?

    EDIT: Disregard. Tabstop went where I was headed!
    Last edited by Matticus; 07-29-2011 at 10:08 AM.

  11. #11
    Registered User
    Join Date
    May 2011
    Posts
    32
    Quote Originally Posted by tabstop View Post
    Because they are. Try changing this erroneous line:
    Code:
    printf("\nYou entered: %i", result);
    to something more correct, like
    Code:
    printf("\nYou entered: %i>>result stops here", result);
    Yep, that's what I overlooked, thanks. And why did I overlook it? Because I didn't get a message in the XCode debug window that the program terminated... Strange. That's why I thought it was waiting for input... Silly me!

    EDIT: The program does terminate but after some 30 seconds or so of being idle after the last output...
    Last edited by kkk; 07-29-2011 at 10:14 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gets(), scanf(), fgets() problem!
    By securetux in forum C Programming
    Replies: 4
    Last Post: 03-27-2011, 02:09 AM
  2. scanf vs fgets?
    By Matus in forum C Programming
    Replies: 65
    Last Post: 11-17-2008, 04:02 PM
  3. what happens after 'fgets' and 'scanf'
    By the bassinvader in forum C Programming
    Replies: 4
    Last Post: 07-30-2006, 03:04 PM
  4. Scanf->Fgets
    By 00Sven in forum C Programming
    Replies: 2
    Last Post: 04-21-2006, 02:39 PM
  5. replacing scanf with fgets
    By guest73 in forum C Programming
    Replies: 8
    Last Post: 09-16-2002, 04:52 AM