我几乎到达了我的代码的结尾,经过大量搜索后我没有找到解决方案,我想为我的程序提供像'\ 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' 作为原始
请有人帮助我。