سؤال

I try to open a file in a function in a class:

void openFile(){

    inputFile.open(inputFilename.c_str());

    if (inputFile.is_open()){
      inputFile.read(buffer, skipAtBegin);
    } else {
      cerr << "Cannot open file: " << inputFilename << endl;
      exitNow();
    }
}

In main() i would just return 1, but how can i do this in a sub-sub-function-class the best/simplest way??

Return 1 all steps up to main?

Use ecxeptions?

Any exit() commands?

هل كانت مفيدة؟

المحلول

Usually there are three Options:

  1. Use exit. Quit the program.
  2. Use exception. Throw exceptions, handles elsewhere.
  3. Set global status variable. Set global status, and let other functions checkt the status and handle it.

Hope someone would come up with other options.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top