Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > [easy] reading into an array

Reply
Thread Tools

[easy] reading into an array

 
 
Susan Sherpi
Guest
Posts: n/a
 
      10-17-2003
Hello,

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.

Can someone tell me what's wrong with this function:

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

I bet there's a standard library function for this, too - is there?

Thanks -Susan
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      10-17-2003
Susan Sherpi wrote:
>
> Hello,
>
> 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.
>
> Can someone tell me what's wrong with this function:
>
> ----------------------------
> 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. So if the first input line has
five numbers, the first five calls to scanf() will consume
and convert them. The sixth call will see the newline, skip
over it, and start trying to read from the second input line.
This use of scanf() will keep on returning 1 until it finds
a non-white character that cannot be part of an integer,
something like an 'x' or a '?' (or end-of-file). As long as
you keep feeding it numbers and white space, it will keep on
consuming them and keep on asking for more.

> I bet there's a standard library function for this, too - is there?


There's probably some way to use scanf() with the "%["
specifier to do what you want, but if so it'll be ugly. 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). You could use sscanf() instead of
strtol(), but it'd be marginally more complicated.

--
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading SQL query into array Steve ASP .Net 1 08-30-2004 08:52 AM
Reading/writing data to/from files into 2D array Modukuri VHDL 0 05-19-2004 04:03 PM
Reading Blank Lines into StringTokens into Vector Array Row Java 0 04-14-2004 07:12 AM
reading from a file into an array Allen Seelye C++ 4 11-07-2003 07:49 PM
Reading a very large textfile into an array Markus Hofmann C++ 3 08-12-2003 04:59 PM



Advertisments