Thread: Error with fgets()

  1. #1
    Registered User
    Join Date
    May 2020

    Error with fgets()

    Hello, good people. I just started learning C and i can't understand how some things work. I believe i got how printf, scanf and fgets work, but can't figure why when im combining them it fails to work. From what i've read, i was supposed to add \n. And i did and it still doesnt work. The problem is that i cant input anything to be stored in fgets. Why is that and how do i fix it?

    char a[20];
    char b[20];
    char c[30];
    printf("Enter a color \n");
    scanf("%s", a);
    printf("Enter a plural noun \n");
    scanf("%s", b);
    printf("Enter your first and last name \n" );
    fgets(c, 30, stdin);
    printf("You've chosen %s\n", a);
    printf("You like %s\n", b);
    printf("Was a pleasure meeting you, %s \n", c);

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Especially since you're reading strings all the way, just use fgets instead of scanf. You'll avoid the buffer overflow vulnerability of using %s without field width that way, at the cost of having to remove a likely '\n' from the end of your strings.

    The reason why you're having trouble now is that scanf with %s leaves the newline from the previous input in the input buffer, which is then read by fgets as if it were a blank line, leaving your actual input in the input buffer.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    scanf and fgets don't play nicely with one another.

    In particular, scanf typically leaves trailing \n on the input stream - which is exactly what fgets looks for to exit early.

    Typically, use fgets() for everything, then use sscanf() on the resulting in-memory copy of the input to do your thing.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    May 2020
    I think i understand how it is now. An important lesson was learned.
    Thank you two for the answers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fgets() / File reading error?
    By aojsfalsie in forum C Programming
    Replies: 2
    Last Post: 08-27-2014, 10:40 PM
  2. fgets error message
    By johnpr in forum C Programming
    Replies: 7
    Last Post: 03-06-2012, 04:40 PM
  3. fopen error & Assertion error - fgets.c
    By andyscouse78 in forum C++ Programming
    Replies: 3
    Last Post: 04-16-2007, 03:33 AM
  4. fgets Error
    By Epo in forum C++ Programming
    Replies: 1
    Last Post: 06-16-2003, 08:44 PM
  5. fgets error handling
    By mlsbbe in forum C Programming
    Replies: 5
    Last Post: 03-24-2003, 02:45 PM

Tags for this Thread