Another case of RTFM
Code:
#include <stdio.h>
#include <unistd.h>
int main(int argc, char **argv)
{
char c, sort_type;
int field;
int rec_count;
char file_in[100];
char file_out[100];
FILE *fp1,*fp2;
/* Everything expects a parameter */
while((c = getopt(argc, argv, "i:o:s:f:")) > 0)
{
printf("Opt=%c, string=%s\n", c, optarg ); /* Add DEBUG */
switch(c)
{
case 'i':
if((sscanf(optarg, "%s", file_in)) != 1)
fprintf(stderr,"Input file was not provided\n");
break;
case 'o':
if((sscanf(optarg, "%s", file_out)) != 1)
fprintf(stderr,"Output file was not provided\n");
break;
case 's':
if((sscanf(optarg, "%c", &sort_type)) != 1)
fprintf(stderr,"Sort type not provided\n");
break;
case 'f':
if((sscanf(optarg, "%d", &field)) != 1)
fprintf(stderr,"Field to be sorted not provided\n");
if(field == 2)
printf("String field selected to be sorted\n");
else
printf("Record Number field selected to be sorted\n");
break;
}
}
return 0;
}
Without this change, I was just getting right at the start a NULL pointer (this is simple debug skills at work)
Code:
spc@sycamore ~
$ ./a.exe -i hello -o world -d 2 -s t
Opt=i, string=(null)
17 [main] a 1112 _cygtls::handle_exceptions: Error while dumping state (probably corrupted stack)
Segmentation fault (core dumped)
$ ./a.exe -ihello -oworld -d2 -st
Opt=i, string=(null)
15 [main] a 2428 _cygtls::handle_exceptions: Error while dumping state (probably corrupted stack)
Segmentation fault (core dumped)
With a : in every place in the opt string, I get
Code:
$ ./a.exe -ihello -oworld -f2 -st
Opt=i, string=hello
Opt=o, string=world
Opt=f, string=2
String field selected to be sorted
Opt=s, string=t
$ ./a.exe -i hello -o world -f 2 -s t
Opt=i, string=hello
Opt=o, string=world
Opt=f, string=2
String field selected to be sorted
Opt=s, string=t