I am attempting to implement command-line options into my code. For some reason, my -a option works correctly, but my -c option does not work well, even though they are basically the same. I get the following message when I try to run my code with the -c option.
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_S_construct NULL not valid
Aborted
Below is my code.
int c;
std::string config = def+std::string("SamplesConfig.xml");
std::string cal = def+std::string("calibration.bin");
while ((c = getopt(argc, argv, "a:c"))>=0)
{
switch(c)
{
case 'a':
{
config = std::string(optarg);
printf("%s", (char *)config.c_str());
break;
}
case 'c':
{
cal = std::string(optarg);
printf("%s", (char *)cal.c_str());
break;
}
default:
{
break;
}
}
}
Shouldn’t you be calling getopt(“a:b:c:”) ? I imagine as you’ve specified just c, optarg will be null when it gets to that point.