Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I read a variable number of integers from the command line? 1

Status
Not open for further replies.

nkrst1

Programmer
Feb 13, 2001
49
US
How would I read variable number of integers from a string? i.e. if the command line was:
3 4 5 2 33 423

how could i make them all individual integers? I've tried using sscanf, but I can't figure out how to get a variable number of variables if there were 3 numbers vs. perhaps 40.
 
you can scanf the line as a string then you seperate these integer number from the string

hope help
 
i used strtok(...) to parse the string... it was much easier that way. I could use the numbers separated by a delimeter (in this case, a space) to extract the individual entries, then used atoi to turn them into ints. Thanks for the reply.
 
that is technical manner,
you can try out to write your own function to seperate!

any way

 
void main(int x, char** c)
{
parm0=c[0];
parm1=c[1];
...
parm[x-1]=c[x-1];

} John Fill
1c.bmp


ivfmd@mail.md
 
If the strings are actually coming from the command line, you would be best off doing as John seems to be suggesting:

#include <stdlib.h>

int main(int argc,char **argv)
{
int i;
int val;

for (i=0;i<argc;++i) {
val=(int)strtol(argv,NULL,10);
/* ... */
}
return 0;
}

If you have the numbers embedded in a string; something like this:

char s[]=&quot;55 4 99 10 5&quot;;

strtok() is a good idea as you seemed to have discovered.

Beware of atoi() though. If you pass it a value that it can't represent as an integer, the behavior is undefined. You can use strtol() and cast the return value to int as shown above instead. If strtol() doesn't like part of the string, it just stops converting.

Russ
bobbitts@hotmail.com
 
Refer to stdargs.h

Use 'valist' option

No time to put the code in here now, but if u want, contact me.

This will allow u to write functions like printf
 
There is another simple approach to this.

You can treat STDIN like a FILE stream and
keep reading from it till you encounter EOF.
EOF would be something like a Ctrl-D character in
a separate line.

So you keep entering your numbers and finish
off with a Ctrl-D on a separate line.


Best Regards,

abp :cool:

The code for this would be;

#include <stdio.h>

main()
{
int val[100];

int i,j;

i=0;
while (!feof(stdin))
{
fscanf(stdin, &quot;%d&quot;, &val[i++]);
}

for (j=0; j<--i; j++)
printf(&quot;%d \n&quot;, val[j]);


}

 
Of course, you'll want to insert some error checking to make sure you don't exceed the bounds of val in abp's example.

Also, feof() doesn't detect end of file, until after the stream has been evaluated, so your fscanf() call will place the value of EOF into val[n] before feof() detects it, which is probably not what you want.

Russ
bobbitts@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top