I almost reached end of my code, after a lot of search I didn't find solution no where, I want to supply escape sequence like '\t', '\n' to my program like how awk
and perl
program takes, and finally I want to use them as printf or sprintf format string
This is what I tried so far, please note I need to have variable delim and rs should be pointer.
#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);
}
When I compile and execute I get output like this
$ 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
I expect it to print tab and newline instead of '\t' and '\n' as original
Kindly someone help me.