Thread: Multiline input from stdin

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    4

    Multiline input from stdin

    Hi everyone,

    I need to read multiple lines (separated by newline) from the stdin stream. If I use fgets(...), it stops reading as soon as it entcounters the first newline (which isn't what I want). I can read from stdin character by character using fgetc(...), but how do I check when the end of input has been reached? comparing fgetc(...) with EOF or '\0' didn't work either. Can anyone help? Can't use '\' at prompt to indicate multiple lines of input either, since ultimately it's not going to be an interactive program.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    You're not limited to 1 fgets() call per program. Just call fgets() in a loop until you have all the input you need.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    4
    I know that I can use fgets(...) in a loop - but what will the terminating condition be? In other words, how do I know that the contents of stdin have been completely read?

  4. #4
    C/C++Newbie Antigloss's Avatar
    Join Date
    May 2005
    Posts
    216
    Code:
    int ch;
    while ( ( ch=getchar() ) != EOF ) {
        /* do something */
    }
    when you finish your input, press ^Z(control + z) if you are using windows, or ^D(control +d) if you are using unix
    for example
    fjlkfaljk
    aaadfffffff
    dfdd
    ^Z
    Last edited by Antigloss; 09-28-2005 at 12:50 AM.

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    4
    I know that I can use fgets(...) in a loop - but what will the terminating condition be? In other words, how do I know that the contents of stdin have been completely read?

  6. #6
    Registered User
    Join Date
    Sep 2005
    Posts
    4
    Yuck, sorry for double posting. Antigloss - thanks for replying - I did say in my first post that it's not going to be an interactive program. But maybe I can append the EOF characater at the end of the string before it gets to stdin. Thanks....

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Mixing fgets() with other input functions (in particular scanf) is a bad move.

    Post actual code if you want a proper answer.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Printing Length of Input and the Limited Input
    By dnguyen1022 in forum C Programming
    Replies: 33
    Last Post: 11-29-2008, 04:13 PM
  2. Reading Input From stdin
    By EvilGuru in forum C Programming
    Replies: 5
    Last Post: 11-25-2005, 01:48 AM
  3. customer input
    By buckwheat88 in forum C Programming
    Replies: 6
    Last Post: 11-21-2005, 08:01 PM
  4. Determining if input is int, double, or char
    By Lucid003 in forum C++ Programming
    Replies: 4
    Last Post: 11-16-2005, 04:16 PM
  5. need help with some input
    By blindleaf in forum C Programming
    Replies: 2
    Last Post: 03-16-2003, 01:50 PM