Thread: gets vs fgets

  1. #1
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463

    gets vs fgets

    Hi,
    I have this while loop that works with gets but not fgets. The purpose is to quit the program when I hit enter which gets return NULL and exit the loop. However, using the same thing with fgets, the program just won't quit but got suck in a loop until count = MAXBKS.

    With gets:

    Code:
    while ( count < MAXBKS && gets(library[count].title) != NULL
    							&& library[count].title[0] !='\0')
    With fgets:

    Code:
    while ( count < MAXBKS && fgets(library[count].title, MAXTITL, stdin) != NULL && library[count].title[0] !='\0')
    please help.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The newline is in the buffer with fgets, so check for it instead of \0.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Thank you it worked.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fgets not working after fgetc
    By 1978Corvette in forum C Programming
    Replies: 3
    Last Post: 01-22-2006, 06:33 PM
  2. problem with fgets
    By learninC in forum C Programming
    Replies: 3
    Last Post: 05-19-2005, 08:10 AM
  3. problem with fgets
    By Smoot in forum C Programming
    Replies: 4
    Last Post: 12-07-2003, 03:35 AM
  4. fgets crashing my program
    By EvBladeRunnervE in forum C++ Programming
    Replies: 7
    Last Post: 08-11-2003, 12:08 PM
  5. help with fgets
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-17-2001, 08:18 PM