line of array from stdin

Discussion in 'C Programming' started by Susan Sherpi, Oct 19, 2003.

  1. Susan Sherpi

    Susan Sherpi Guest

    Hi. Thanks to Eric Sosman for showing me what was wrong with my code in an
    earlier posting. He wrote, quoting me:

    >> I want to read a line of decimal integers from standard input into an
    >> array, stopping when a newline is encountered, and I'd like the
    >> function to return the number of values read in.
    >>
    >> ----------------------------
    >> int read_array(int p[])
    >> {
    >> j=0;
    >> while(scanf("%d",&p[j])==1)
    >> j++;
    >> return j;
    >> }
    >> ----------------------------
    >>
    >> I thought that as soon as scanf found a character that wasn't a decimal
    >> number, it wouldn't be able to put it in the array and would return 0,
    >> and I'd bust out of the while loop - but that doesn't happen.

    >
    > As used here, scanf() skips white space -- and newlines
    > are considered white space.



    Ah yes that seems obvious now. I suppose I could just have the user end a
    line of input with a random non-whitespace non-numeric character - would
    make my life easier, but seems like a dumb solution.


    > I'd suggest using two functions: read an entire single line with
    > fgets(), and then use strtol() repeatedly to convert the numbers
    > (and check for garbage).


    Thanks. First I tried using sscanf on the output of fgets, but it just
    kept reading the first number in the string, of course... I see strtol is
    designed for this, great, but the documentation I have on strtol is so
    terse, I'm afraid I can't figure out how to call it repeatedly. I
    understand it reads as much of the string as it can convert into a number,
    then returns a pointer to the beginning of the unused portion of the
    string so the next call can start where the last one left off. Could some
    kind soul show me an example of repeated calls to strtol? And can I have
    strtol use both whitespace and commas as alternative field delimiters?

    Susan
    Susan Sherpi, Oct 19, 2003
    #1
    1. Advertising

  2. Susan Sherpi

    Malcolm Guest

    "Susan Sherpi" <> wrote in message
    > I see strtol is designed for this, great, but the documentation I have on
    > strtol is so terse, I'm afraid I can't figure out how to call it

    repeatedly.

    /*
    read a line of integers from stdin.
    Inputs ret - return pointer for integers read.
    len - size of the buffer (for safety)
    Returns: No integer read if successful, -1 on error
    */
    int readint(int *ret, int len)
    {
    char buff[1024];
    char *ptr;
    char *tmp;
    int answer = 0;

    /* read a line, return -1 if error */
    if( fgets(buff, 1024, stdin) == NULL)
    return -1;

    /* return -1 if fgets() didn't read the whole line */
    if(!strchr(buff, '\n'))
    return -1;

    ptr = buff;
    /* skip leading whitespace */
    while(isspace(*ptr))
    ptr++;

    /* do out loop until ptr points to the terminating NUL */
    while(*ptr)
    {
    /* read an integer */
    /* the pointer tmp holds the first non-digit strtol encountered */
    ret[answer] = (int) strtol(ptr, &tmp, 10);
    /* if tmp equals ptr we have garbage and not a number */
    if(tmp == ptr)
    return -1;
    ptr = tmp;
    answer++;
    /* here's a bit for you to do. You need to check for whitespace and
    commas, and skip ptr so it jumps over them */
    /* you also want to check that answer isn't greater or equal to len */
    }
    /* return the number of integers we read */
    return answer;
    }
    Malcolm, Oct 19, 2003
    #2
    1. Advertising

  3. Susan Sherpi

    CBFalconer Guest

    Susan Sherpi wrote:
    >
    > Hi. Thanks to Eric Sosman for showing me what was wrong with my
    > code in an earlier posting. He wrote, quoting me:
    >
    > >> I want to read a line of decimal integers from standard input
    > >> into an array, stopping when a newline is encountered, and
    > >> I'd like the function to return the number of values read in.
    > >>

    .... snip ...
    >
    > > I'd suggest using two functions: read an entire single line with
    > > fgets(), and then use strtol() repeatedly to convert the numbers
    > > (and check for garbage).

    >
    > Thanks. First I tried using sscanf on the output of fgets, but it
    > just kept reading the first number in the string, of course... I
    > see strtol is designed for this, great, but the documentation I


    To make sscanf advance, try the following. You can get the source
    of ggets at:

    <http://cbfalconer.home.att.net/download/>

    #include <stdio.h>
    #include <stdlib.h>
    #include "ggets.h"

    int countints(void)
    {
    char *ln;
    int num;
    int count;
    int ix, delta;

    count = 0; ix = 0;
    if (0 == ggets(&ln)) {
    while (1 == sscanf(&ln[ix], "%d%n", &num, &delta)) {
    printf("[%d]%d ", ix, num);
    ix += delta;
    count++;
    }
    printf(": %d\n", count);
    free(ln);
    }
    return count;
    } /* countints */

    /* ------------------ */

    int main(void)
    {
    while (countints()) continue;
    return 0;
    } /* main */

    You can easily gussy things up to store the values in an array.

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
    CBFalconer, Oct 20, 2003
    #3
  4. Susan Sherpi

    Eric Sosman Guest

    Malcolm wrote:
    >
    > "Susan Sherpi" <> wrote in message
    > > I see strtol is designed for this, great, but the documentation I have on
    > > strtol is so terse, I'm afraid I can't figure out how to call it

    > repeatedly.
    >
    > /*
    > read a line of integers from stdin.
    > Inputs ret - return pointer for integers read.
    > len - size of the buffer (for safety)
    > Returns: No integer read if successful, -1 on error
    > */
    > int readint(int *ret, int len)
    > {
    > char buff[1024];
    > char *ptr;
    > [...]
    > ptr = buff;
    > /* skip leading whitespace */
    > while(isspace(*ptr))
    > ptr++;
    > [... and then apply strtol(ptr,...) ...]


    There are two problems here: One outright error and one
    inefficiency.

    The error is that isspace() and friends take an `int'
    argument whose value is either EOF (not an issue here) or
    the value of an `unsigned char'. On a system where plain
    `char' is signed, the conversion of `*ptr' to an `int'
    can produce negative values, which are clearly not within
    the range of an `unsigned char', and demons will fly out
    of your nose. Write instead

    while (isspace( (unsigned char)*ptr ))

    The inefficiency is that strtol() skips leading white
    space all by itself without external help; the loop is
    altogether unnecessary! (I imagine it was inserted in
    the sample code to satisfy a requirement laid down by
    the Department of Redundancy Dept.)

    --
    Eric Sosman, Oct 20, 2003
    #4
  5. Susan Sherpi

    Susan Sherpi Guest

    CBFalconer <> wrote:
    > Susan Sherpi wrote:
    >>
    >> Hi. Thanks to Eric Sosman for showing me what was wrong with my
    >> code in an earlier posting. He wrote, quoting me:
    >>
    >> >> I want to read a line of decimal integers from standard input
    >> >> into an array, stopping when a newline is encountered, and
    >> >> I'd like the function to return the number of values read in.
    >> >>

    > ... snip ...
    >>
    >> > I'd suggest using two functions: read an entire single line with
    >> > fgets(), and then use strtol() repeatedly to convert the numbers
    >> > (and check for garbage).

    >>
    >> Thanks. First I tried using sscanf on the output of fgets, but it
    >> just kept reading the first number in the string, of course... I
    >> see strtol is designed for this, great, but the documentation I

    >
    > To make sscanf advance, try the following. You can get the source
    > of ggets at:
    >
    > <http://cbfalconer.home.att.net/download/>
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    > #include "ggets.h"
    >
    > int countints(void)
    > {
    > char *ln;
    > int num;
    > int count;
    > int ix, delta;
    >
    > count = 0; ix = 0;
    > if (0 == ggets(&ln)) {
    > while (1 == sscanf(&ln[ix], "%d%n", &num, &delta)) {



    Thanks --what is the %n format specifier? it's not on my list

    Susan
    Susan Sherpi, Oct 20, 2003
    #5
  6. Susan Sherpi

    Ed Morton Guest

    On 10/20/2003 11:18 AM, Susan Sherpi wrote:
    > CBFalconer <> wrote:
    >

    <snip>
    >> while (1 == sscanf(&ln[ix], "%d%n", &num, &delta)) {

    >
    >
    >
    > Thanks --what is the %n format specifier? it's not on my list


    You can look it up on-line at:

    http://www.acm.uiuc.edu/webmonkeys/book/c_guide/2.12.html#scanf

    Regards,

    Ed.

    > Susan
    Ed Morton, Oct 20, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. G Kannan
    Replies:
    1
    Views:
    1,233
    Eric J. Roode
    Oct 11, 2003
  2. Johnathan Doe

    peek at stdin, flush stdin

    Johnathan Doe, May 15, 2004, in forum: C Programming
    Replies:
    5
    Views:
    24,951
    Chatoyer
    May 17, 2013
  3. Charlie Zender

    Reading stdin once confuses second stdin read

    Charlie Zender, Jun 19, 2004, in forum: C Programming
    Replies:
    6
    Views:
    785
    Dan Pop
    Jun 21, 2004
  4. RG
    Replies:
    20
    Views:
    992
    Nobody
    Aug 12, 2010
  5. Michael Goerz

    handling STDIN line by line in Gtk

    Michael Goerz, Feb 11, 2007, in forum: Perl Misc
    Replies:
    5
    Views:
    267
    Michael Goerz
    Feb 12, 2007
Loading...

Share This Page