The strtok()
returns pointer, so you have to declare parampart
and paramvalue
as pointers, like
char *parampart;
char *paramvalue;
the rest of your code is correct.
Question
I am new to C and I am trying to pass arguments to my program like
program_name -param1=something -param2=somethingelse
Then in my program I want to loop through the arguments and split them on the "=" and print the two parts back to the command line. Here is what I have so far
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
int i = 0;
char parampart;
char paramvalue;
for(i = 0; i < argc; i++)
{
parampart = strtok(argv[i], "=");
paramvalue = strtok(NULL, "=");
printf("parampart: %s paramvalue %s", parampart, paramvalue);
}
return 0;
}
I am getting errors because the variables parampart and paramvalues are pointers but I'm not sure how to use the pointers to get the string values.
Solution
The strtok()
returns pointer, so you have to declare parampart
and paramvalue
as pointers, like
char *parampart;
char *paramvalue;
the rest of your code is correct.
OTHER TIPS
If you are using Linux you can use getopt. it make life easier
The problem is that you're assuming each argument will have a =
in it. Which most of them do.... but not the zero'th one, which is program_name
. You should start at arg 1, not arg 0, and you should check for a null return from the second strtok
call, in case the user forgets the equals-sign.
Of course, as @MOHAMED mentioned, this is a job for getopt.
It's a good example from man strtok
, you should call once befroe your loop:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int
main(int argc, char *argv[])
{
char *str1, *str2, *token, *subtoken;
char *saveptr1, *saveptr2;
int j;
if (argc != 4) {
fprintf(stderr, "Usage: %s string delim subdelim\n",
argv[0]);
exit(EXIT_FAILURE);
}
for (j = 1, str1 = argv[1]; ; j++, str1 = NULL) {
token = strtok_r(str1, argv[2], &saveptr1);
if (token == NULL)
break;
printf("%d: %s\n", j, token);
for (str2 = token; ; str2 = NULL) {
subtoken = strtok_r(str2, argv[3], &saveptr2);
if (subtoken == NULL)
break;
printf(" --> %s\n", subtoken);
}
}
exit(EXIT_SUCCESS);
}
And according to man getopt
:
#include <stdio.h> /* for printf */
#include <stdlib.h> /* for exit */
#include <getopt.h>
int
main(int argc, char **argv)
{
int c;
int digit_optind = 0;
while (1) {
int this_option_optind = optind ? optind : 1;
int option_index = 0;
static struct option long_options[] = {
{"add", required_argument, 0, 0 },
{"append", no_argument, 0, 0 },
{"delete", required_argument, 0, 0 },
{"verbose", no_argument, 0, 0 },
{"create", required_argument, 0, 'c'},
{"file", required_argument, 0, 0 },
{0, 0, 0, 0 }
};
c = getopt_long(argc, argv, "abc:d:012",
long_options, &option_index);
if (c == -1)
break;
switch (c) {
case 0:
printf("option %s", long_options[option_index].name);
if (optarg)
printf(" with arg %s", optarg);
printf("\n");
break;
case '0':
case '1':
case '2':
if (digit_optind != 0 && digit_optind != this_option_optind)
printf("digits occur in two different argv-elements.\n");
digit_optind = this_option_optind;
printf("option %c\n", c);
break;
case 'a':
printf("option a\n");
break;
case 'b':
printf("option b\n");
break;
case 'c':
printf("option c with value '%s'\n", optarg);
break;
case 'd':
printf("option d with value '%s'\n", optarg);
break;
case '?':
break;
default:
printf("?? getopt returned character code 0%o ??\n", c);
}
}
if (optind < argc) {
printf("non-option ARGV-elements: ");
while (optind < argc)
printf("%s ", argv[optind++]);
printf("\n");
}
exit(EXIT_SUCCESS);
}
I suggest you set flag for each option and initilize them according to behave your program.