13

I am reading an input file from the command line.

int main(int argc, char **argv)
{
    Scene myScene;
string filename = argv[1];
myScene = Parser(filename);
 ...
}

from another file I use the parser function which is declerated like that;

Scene Parser(string filename)
{
 string line;
 ifstream myfile (filename.c_str());
 ...
 return scene;
}

I am getting the error; terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_S_construct null not valid

Program received signal SIGABRT, Aborted.

I have searched the error. I think it is because of these lines. But I cannot find the actual reason. Can anybody help me?

1
  • 4
    You must never say argv[1] if you don't check argc > 1 beforehand. Commented Jan 11, 2014 at 22:41

2 Answers 2

11

This means that filename is NULL in Parser, probably because you are not passing any arguments to your program's command line.

Make sure to always check whether the expected number of arguments are passed to your program. For instance, you could do :

int main(int argc, char *argv[]) {
   if (argc != NUMBER_OF_EXPECTED_ARGUMENTS) {
      exit(EXIT_FAILURE);
   }
   // ...
   string filename(argv[1]);
   Scene myScene = Parser(filename);
   // ...
}
Sign up to request clarification or add additional context in comments.

Comments

5

It is possible that you forgot to specify command line arguments and as the result argv[1] is equal to NULL. You should check whether the user entered command line arguments. For example

int main(int argc, char **argv)
{
    Scene myScene;
    string filename;
    if ( 1 < argc ) filename.assign( argv[1] );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.