Thread: help with fgets

  1. #1
    Unregistered
    Guest

    Angry help with fgets

    /* 1: why is the second printf on a newline */
    /* 2: each fgets reads 10 chars and retains the '\n' if I input say */
    /* qwertyuiop why is the second fgets skipped I think this has something to */
    /* do with retaining the '\n' from above, but what?*/
    /* 3: Why does'nt the third printf print on the same line */
    /* 4: And finally if more chars are entered than fgets can accept */
    /* are they automatically flushed from fgets because it adds '\0' */

    #include<stdio.h>
    #include<conio.h>

    int main()
    {
    char first_name[10];
    char last_name[10];
    int age;

    printf("Input first name :>");
    fgets(first_name,11,stdin);

    printf("Input last name :>");
    fgets (last_name,11,stdin);

    printf("Input age :>");
    scanf("%d",&age);

    printf("first name :>%s last_name :>%s age :>%d",first_name,last_name,age);

    getch();

    return 0;
    }

    /* Thanks for the help all. */

  2. #2
    Registered User Engineer's Avatar
    Join Date
    Oct 2001
    Posts
    125

    Exclamation

    1: why is the second printf on a newline?

    It is on a new line because by fgets() moves the cursor to the new line after reading the input.

    2: each fgets reads 10 chars and retains the '\n' if I input say
    qwertyuiop why is the second fgets skipped I think this has something to
    do with retaining the '\n' from above, but what?

    I really have no time to run and debug your code, but I can tell you this:
    the behaviour of your code might become completely unpredictible when you use memory that doesn't belong to you. I have had the similar problem at work yesterday. This is what you are doing wrong - you want to scan in 10 characters, and you are allocating only 10 bytes for this. You should be allocating 11 bytes (for 10 chars + '\0'). So causes fgets() to use out-of-bounds memory.

    3: Why does'nt the third printf print on the same line?

    Same as in Q.1.

    4: And finally if more chars are entered than fgets can accept
    are they automatically flushed from fgets because it adds '\0'?

    fgets() reads the number of chars specified or until '\n' is reached. I am not too sure about fgets() flushing the unnecessary chars automatically. I would not assume that it is, because most functions that deal with strings are usually pretty unreliable and can cause a buffer overrun (what you are experiencing in this code).
    1 rule of the Samurai Code: if you have nothing to say, don't say anything at all!

  3. #3
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    char first_name[10];
    ...
    printf("Input first name :>");
    fgets(first_name,11,stdin);
    This question has been answered but here it is again. You overwrote your string. Change it to:

    fgets(first_name,10,stdin);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strange behavior if I use fgets() after fscanf()
    By avi2886 in forum C Programming
    Replies: 5
    Last Post: 02-17-2009, 04:20 PM
  2. fgets not working after fgetc
    By 1978Corvette in forum C Programming
    Replies: 3
    Last Post: 01-22-2006, 06:33 PM
  3. problem with fgets
    By learninC in forum C Programming
    Replies: 3
    Last Post: 05-19-2005, 08:10 AM
  4. problem with fgets
    By Smoot in forum C Programming
    Replies: 4
    Last Post: 12-07-2003, 03:35 AM
  5. fgets crashing my program
    By EvBladeRunnervE in forum C++ Programming
    Replies: 7
    Last Post: 08-11-2003, 12:08 PM