I'm fairly new to the getopt function and anything it involves. At the moment I'm trying to write a simple application that has two parameters and additional arguments. The problem is that the output from one of the variables (parm) is only a bunch of strange symbols. Here's the code:
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
extern char *optarg;
extern int optind;
extern int optopt;
extern int opterr;
extern int optreset;
int main(int argc, char *argv[]){
int command;
char cmnd[25];
char parm[25];
while((command = getopt(argc, argv, "c:p:")) != -1) {
switch(command) {
case 'c':
strcpy(cmnd, optarg);
break;
case 'p':
strcpy(parm, optarg);
break;
case '?':
default:
break;
}
printf("command: %s\n", cmnd);
printf("parameter: %s\n", parm);
}
return 0;
}
And here's an example of an output:
Code:
./printArgs -c test -p TEST
command: test
parameter: ??P????P
command: test
parameter: TEST
Something is obviously not correct here, help highly appreciated.