Command line args

My program usage takes the form for example;
$ theApp 2 "one or more words"

i.e. 3 command line arguments; application name, an integer, some text

My code includes the following 4 lines:

int anInteger;
char words[80];

sscanf(argv[1], "%d", &anInteger);
sscanf(argv[2], "%s", &message);

Based on th example execution shown above this code results in th following assignments:

anInterger = 2 /* correct and as I thought*/
message = one /* not as I thought or intended*/

I thought when I printed out the value of message I would get:
one or more words

but instead I get:
one

I have done a test and the use of the " " marks seems to make "one or more words" be seen as a single argument. If I don't use the " " marks it sees every word as another argument.

So even though the program sees "one or more words" as a single argument, sscanf is not reading it properly. How can I overcome this problem?

That is how the scanf family of functions works. Try another test:

#include<stdio.h>

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

        fscanf(stdin,"%s",message);
        fprintf(stdout,"%s\n",message);
}

If you run this, this is what you get:

# ./a.out
this is a test
this

This is how it is implemented. Anyway, if you want to copy the second argument (argv[2]) into the message string, just use strcpy, like this:

#include<stdio.h>
#include<string.h>

int main(int argc, char *argv[]){
        int anInteger;
        char message[80];

        sscanf(argv[1], "%d", &anInteger);
        strcpy(message,argv[2]);
        fprintf(stdout,"anInteger: %d\nmessage: %s\n",anInteger,message);
}

This works as you want it to:

# ./a.out 2 "this is a test"
anInteger: 2
message: this is a test

Thanks very much, problem solved.