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.)
--