George McFlying
George McFlying

Reputation: 71

How to use sscanf with argv

I want to get input from the command line, (n) but my code is only writing this progress bar. I can't figure out how to get it to print a statement like Enter a value and then take that value and run the program with it. I have tried putting it so many different places. Any suggestions?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>

int main(int argc, char *argv[]) {
    //Check command line
    if (argc != 2) {
        printf("Usage: progress n\n");
        return 1;
    }

    int n = atoi(argv[1]);
    time_t c = atoi(argv[1]);
    int go = atoi(argv[1]);

    if (argc == 2) {
        //sscanf(argv[1], "%d", &n);
        time(&c);
        n = 10;
        go = n + c;

        char display[70];
        char equals[70];
        char disp[70];

        for (int i = 0; i <= go * n; i++) {
            strcpy(display, "progress: |");
            strcat(equals, "=");
            strcpy(disp, "|");

            char number[70];
            char space[70];

            sscanf(argv[1], "%d", &n);
            sprintf(number, "  %d%%", i * 2);
            sprintf(space, "%-52s", equals); // this keeps the 2nd bar static
            strcat(display, space);

            strcat(display, disp);
            strcat(display, number);
            //This is the final output
            fprintf(stderr, "\r%s %d  \r%s", equals, i, display);

            usleep(1000000);
        }
    }
    return 0;
}

Upvotes: 3

Views: 9907

Answers (1)

chqrlie
chqrlie

Reputation: 144780

To convert a command line argument string argv[1] representing a number to an int n, you can use any of these:

  • n = atoi(argv[1]); This one is simple for numbers in base 10.

  • n = strtol(argv[1], NULL, 10); This one can be used to parse suffixes or check if the argument is really a number. It can support other bases or octal and hexadecimal syntax.

  • n = 0; sscanf(argv[1], "%d", &n); This method can be used too, but n must be initialized or sscanf return value must be checked to avoid undefined behavior for non numeric string arguments.

These methods have subtile differences in how they handle non numeric values and out of range values.

You can use atoi() for your purpose.

If your program is supposed to take 3 command line arguments, you must pass them on the command line. To run the program, open a CMD.EXE terminal on Windows or a terminal window on linux, change the current directory to that of your executable and type:

C:\Project\Myproject> Myproject 123 456 789

Or on linux or Mac/OS:

user@mylaptop:~/projects/myproject > ./myproject 123 456 789

At the start of the main function, check the minimum number of command line arguments:

 if (argc < 3 + 2) {
     printf("missing arguments: expected n c and go\n");
     return 1;
 }

And convert each argument into the corresponding variables:

 int n = atoi(argv[1]);    // first argument is the value of n
 time_t c = atoi(argv[2]); // second argument is the value of c
 int go = atoi(argv[3]);   // third argument is the value of go

Upvotes: 2

Related Questions