fgets with newline in the middle of the stdin

This is a discussion on fgets with newline in the middle of the stdin within the C Programming forums, part of the General Programming Boards category; so in my program i have: Code: fgets(string, 80, stdin); When I copy "cat [\n] dog" into the command line ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 09: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, 12:41 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21