1

我几乎到达了我的代码的结尾,经过大量搜索后我没有找到解决方案,我想为我的程序提供像'\ t','\ n'这样的转义序列,就像程序的方式一样awkperl最后我想将它们用作 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

4

某处的某些代码必须将反斜杠-t 和反斜杠-n 转换为制表符和换行符。您可以让 shell 执行此操作(如果它是 Bash 或支持ANSI C 引用):

./a.out --delim=$'\t'
./a.out --delim=$'\t' --row_sep=$'\n'

或使用printf命令(与功能不同,但与之相关printf());这避免了使用任何 Bashisms:

./a.out --delim="$(printf '\t')"
./a.out --delim="$(printf '\t')" --row_sep="$(printf '\n')"

或者,实际上,您可以简单地在命令行中键入字符。进入选项卡需要您键入Control-VControl-I以避免文件名完成。

$ ./a.out --delim='^V^I'
$ ./a.out --delim='^V^I' --row_sep='
> '

不过,这不太清楚。我会优先使用前两种机制之一。

或者你可以在你的程序中做到这一点。这有点难,但并不多。我有一个相当全面的功能,cstrlit_chr()可以完成大部分工作(它不处理像\u0123or那样的 Unicode 转义\U00012345),但它不是标准的,它位于一个包含注释和测试代码等的 238 行长的文件中(函数中只有 100 多行非空白、非注释的 C 代码行,如果我想花时间在上面可以压缩一点),所以在这里添加它有点大。


你能告诉我cstrlit_chr()同样的例子吗?

记录界面的标题说:

/* Convert C String Literal in (str..end] (excluding surrounding quotes) */
/* to string, returning length of string, or -1 if conversion error, or */
/* -2 if there is not enough room for the output */
extern int cstrlit_str(const char *str, const char *end, char *buffer, size_t buflen);
/* Convert C Character Literal in (str..end] (excluding surrounding quotes) */
/* to character, returning converted char or -1 if string is invalid. */
/* If non-null, eptr is set to first non-converted (or non-convertible) character */
extern int cstrlit_chr(const char *str, const char *end, char const ** const eptr);

/* Convert character to C Character Literal. */
/* buffer[0] = '\0' if there isn't enough room in buffer */
extern void chr_cstrlit(unsigned char c, char *buffer, size_t buflen);
/* Convert string to C String Literal */
extern void str_cstrlit(const char *str, char *buffer, size_t buflen);

所以,cstrlit_chr()是一组四个功能之一。但是,它非常易于使用:

 const char *endptr;
 int c = cstrlit_char(argv[i], argv[i]+strlen(argv[i]), &endptr);

如果argv[i]包含反斜杠和t,那么c将被赋值'\t'(通常是 control-I 或 9)。如果它包含反斜杠和n,那么c将被赋值'\n'(通常是 control-J 或 10)。

in 的值endptr告诉您下一个要解释的字符是什么。

于 2015-02-20T04:00:12.207 回答