0

So I am attempting to read in values from the command land after a flag has been hit. I don't want to use scanf because it shouldn't pause. It should take in arguments as follows: run -p 60 10 Where 60 is a percentage value, and 10 is a number of processes. How does one go about reading those into variables without using scanf which pauses for user input? Do I need to assign them values using argv[2] and argv[3]? I want integer values, not strings.

This is my code so far:

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>


bool Gamble(int percent);




int main(int argc, char *argv[])
{

int c;
int pflag = 0;
int vflag = 0;
int percent;
int processes;

while ((c = getopt (argc, argv, "-p-v: ")) != -1)
{
     switch (c)
       {
       case 'p':
         pflag = 1;
         percent = argv[2];
         //scanf("%d", &percent);
         break;
       case 'v':
         vflag = 1;
         break;
       default:
         processes = argv[3];
       }

}

printf("%d\n", percent);
printf("%d\n", processes);




Gamble(percent);


return 0;

}

P.S. Gamble is just a class that takes in the percentage, generates a random number, and returns "Success" or "Failure" based on whether or not the percentage passed is hit using the random generator.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Necrode
  • 51
  • 2
  • 7
  • possible duplicate of http://stackoverflow.com/questions/4796662/how-to-take-integers-as-command-line-arguments – Dennis Meng Sep 27 '13 at 17:23
  • Would it work for you to get the next input line from a set of stored lines in a file?... using fopen, then call `fgets(,,);` each time your flag is set. or, if argv[] is guaranteed to be numbers, you could convert each of your argv[n] values to int using atoi(n) – ryyker Sep 27 '13 at 17:32
  • I ran out of time while editing my last comment: you could do something like int x[n]; then x[i] = atoi(argv[i]), for 0 to n arguments. – ryyker Sep 27 '13 at 17:42

2 Answers2

2

You can use sscanf to read a number value from a string.

Gavin Smith
  • 3,076
  • 1
  • 19
  • 25
  • Hmmm I must not be using it correctly. I keep getting "0" for both values: `sscanf(argv[2],"%s %*s %d",str, &percent);` `sscanf(argv[3],"%s %*s %d",str, &processes);` – Necrode Sep 27 '13 at 17:26
  • The command-line is split through the `argv` array. E.g. `argv[1]` could be `"-p"`, and `argv[2]` could be `"60"`. – Gavin Smith Sep 27 '13 at 17:28
  • `argv[2]` is 60, I checked. So shouldn't that read `"60"` into `"percent"`? Or am I using sscanf wrong? I looked up the reference page. – Necrode Sep 27 '13 at 17:32
  • 1
    If 60 were in `argv[2]`, you'd want `sscanf(argv[2],"%d", &percent);` instead of what you wrote. However, see koodawg's answer. – Gavin Smith Sep 27 '13 at 17:49
1

getopt puts the option values in optarg. You don't want to read them from argv[] directly.Ssee examples of how to use getopt

http://www.gnu.org/software/libc/manual/html_node/Example-of-Getopt.html
Mike Makuch
  • 1,788
  • 12
  • 15