0

私はCが初めてで、次のようにプログラムに引数を渡そうとしています

program_name -param1=something -param2=somethingelse

次に、私のプログラムでは、引数をループして「=」で分割し、2 つの部分をコマンド ラインに出力します。これが私がこれまでに持っているものです

#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;
}

変数 parampart と paramvalues はポインターであるため、エラーが発生しますが、ポインターを使用して文字列値を取得する方法がわかりません。

4

4 に答える 4

2

Linux を使用している場合は、getoptを使用できます。それは生活を楽にする

于 2013-10-08T14:16:48.407 に答える
1

問題は、各引数に が含まれていると想定していることです=。それらのほとんどは....しかし、ゼロ番目のものではありませんprogram_name。arg 0 ではなく arg 1 から開始する必要がありますstrtok。また、ユーザーが等号を忘れた場合に備えて、2 番目の呼び出しから null が返されるかどうかを確認する必要があります。

もちろん、@MOHAMED が述べたように、これは getopt の仕事です。

于 2013-10-08T14:17:19.973 に答える
0

の良い例man strtokです。ループの前に一度呼び出す必要があります。

   #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);
   }

そしてによると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);
}

各オプションにフラグを設定し、プログラムの動作に従って初期化することをお勧めします。

于 2013-10-08T14:28:14.093 に答える