1

私はほとんどコードの終わりに達しました。多くの検索の後、どこにも解決策が見つかりませんでした。「\ t」、「\ n」などのエスケープシーケンスをプログラムにどのようにawk、そしてperlプログラムが取るかのように提供したいのですが、最後に私はそれらをprintfまたはsprintfフォーマット文字列として使用したい

これは私がこれまでに試したことです。変数 delim が必要であり、rs はポインターである必要があることに注意してください。

 #include <stdio.h>
 #include <stdlib.h>
 #include <getopt.h>


 int main (int argc, char **argv)
 {
   int c;

   char *delim = ",", *rs = "\n\r";

   while (1)
     {
       static struct option long_options[] =
         {

           {"delim",  required_argument, 0, 'd'},
           {"row_sep",  required_argument, 0, 'r'},
           {0, 0, 0, 0}
         };

       int option_index = 0;

       c = getopt_long (argc, argv, "df",
                        long_options, &option_index);


       if (c == -1)
         break;

       switch (c)
         {
         case 0:

           if (long_options[option_index].flag != 0)
             break;
           printf ("option %s", long_options[option_index].name);
           if (optarg)
             printf (" with arg %s", optarg);
           printf ("\n");
           break;

         case 'd':
           delim = optarg;
           break;

         case 'r':
           rs = optarg;
           break;

         case '?':
           break;

         default:
           abort ();
         }
     }


   /* Print any remaining command line arguments (not options). */
   if (optind < argc)
     {
       printf ("non-option ARGV-elements: ");
       while (optind < argc)
         printf ("%s ", argv[optind++]);
       putchar ('\n');
     }



 /* Test input argument */


 printf("This is test%ssome text%s",delim,rs);



   exit (0);
 }

コンパイルして実行すると、次のような出力が得られます

 $ gcc argument.c

 $ ./a.out --delim="\t"
 This is test\tsome text

 $ ./a.out --delim="\t" --row_sep="\n"
 This is test\tsome text\n

元の「\t」と「\n」ではなく、タブと改行を印刷することを期待しています

誰か助けてください。

4

1 に答える 1