Thread: fgets with newline in the middle of the stdin

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    3

    fgets with newline in the middle of the stdin

    so in my program i have:

    Code:
    fgets(string, 80, stdin);
    When I copy "cat [\n] dog" into the command line and print string, string is "cat". "dog" does not seem to be consumed by fgets because next time I press enter string prints as "dog".


    How can I make it so that fgets stores the entire "cat[\n]dog" into string?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    You can't.

    You have to call fgets() in a loop until you have found some other indication that you've reached the end of the input.
    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.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    3
    Ok thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fgets() from stdin and strcat()
    By rocketman03 in forum C Programming
    Replies: 9
    Last Post: 11-01-2008, 01:31 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  4. using stdin in fgets
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 04-14-2002, 09:14 PM
  5. fgets and a bothersome newline char
    By ivandn in forum Linux Programming
    Replies: 1
    Last Post: 11-14-2001, 01:41 PM