Thread: Question About Reading Lines from stdin

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    69

    Question About Reading Lines from stdin

    Hello,

    I was just wondering if it's possible to do the following. Is it possible to use fgets to read in lines from stdin more than once? For example, could you go:

    Code:
     while (fgets(buf,BUFSIZ,stdin) != NULL)
       // do something
    
    while ( fgets(buf2,BUFSIZ,stdin) != NULL)
        //do something else
    Basically, I want to know if it's possible to read in lines from stdin once, and then read in lines again from the beginning? I thought of this because I was thinking of a way to calculate the size of input given from stdin, and then doing something else with the lines from stdin later on. Does this even make sense? Thanks for the help.

  2. #2
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Once you've read the line, the line has been read and can't be read again. Since you read it into a buffer (I assume) simply use the buffer again.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    69
    Could the sizeof function be used on stdin to calculate the size of the input? For instance:

    Code:
     int size = sizeof(stdin);
    Is this valid? Or is there some other way to determine the size of the input?

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    No.

    That will return the size of the stdin pointer.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    69
    Ok. Now how about something completely different?

    If I read in a line from stdin, use strtok to tokenize it, and then use atoi to change the string to an int, if the tokenized string has a \n character on the end of it, will that mess up atoi? Say the string is 10\n and then I used atoi on it. Normally I would just tokenize and use a \n as a delimiter, but for the problem I'm about to work on, I won't know how many tokens will be on each line. Sorry for all the questions, but I'm trying to get things sorted out in my head before I dive into this problem. Thanks.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Zildjian
    Ok. Now how about something completely different?

    If I read in a line from stdin, use strtok to tokenize it, and then use atoi to change the string to an int, if the tokenized string has a \n character on the end of it, will that mess up atoi? Say the string is 10\n and then I used atoi on it.
    Why don't you just try it and find out?
    Code:
    void foo( void )
    {
         printf("%d\n", atoi( "10\n" ) );
    }
    Originally posted by Zildjian
    Normally I would just tokenize and use a \n as a delimiter, but for the problem I'm about to work on, I won't know how many tokens will be on each line. Sorry for all the questions, but I'm trying to get things sorted out in my head before I dive into this problem. Thanks.
    Why don't you just remove the \n first?
    Code:
    if( (ptr=strchr( buffer, '\n' ) != NULL )
        *ptr = '\0';
    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    .
    Join Date
    Nov 2003
    Posts
    307
    A. getc() moves forward in a stream and ungetc() moves backward.

    So it is possible, although not necessarily desirable, to read the same line (or a group of characters) from stdin - twice

    B. length of input line - use strlen() in string.h
    Code:
    char tmp[256];
    memset(tmp,0x00,sizeof(tmp));
    if(fgets(tmp,255,stdin)!=NULL) printf("length=%u\d\n",strlen(tmp));
    C. atoi(), atof(), strtod() all ignore control characters and spaces. They stop reading the string when they encounter one.

    You should get a copy of Herbert Schild's book 'C The complete reference' or something like it. It's organized so that if you look in the File I/O section for a few minutes you could have found ungetc()

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by jim mcnamara
    You should get a copy of Herbert Schild's book 'C The complete reference' or something like it.
    *reels in horror* Nooooooooooooooooo!

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question on reading matrices from file
    By chinesepirate in forum C Programming
    Replies: 1
    Last Post: 09-29-2008, 12:45 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Question regarding reading data from file into arrays
    By vutek0328 in forum C Programming
    Replies: 3
    Last Post: 05-29-2007, 09:20 AM
  4. Line Counting
    By 00Sven in forum C Programming
    Replies: 26
    Last Post: 04-02-2006, 08:59 PM
  5. Brief question about stdin
    By B-Con in forum C Programming
    Replies: 6
    Last Post: 05-31-2004, 07:34 AM