2

im trying to parse a file. however, the ifstream object keeps failing. in my parsing header file i have this:

class ParseDFA {
public:
  bool stop_program = false;
  std::ifstream dfa_file;
  ParseDFA(std::string parse_filename);
};

in my parsing cpp file i have this:

ParseDFA::ParseDFA(std::string parse_filename) {
  dfa_file.open(parse_filename, std::ifstream::in);

  if (dfa_file.fail()) {
    stop_program = true;
  }
}

and in my main.cpp file i have this:

std::string filename = "no000.dfa";
ParseDFA *dfa_file = new ParseDFA(filename);
if (dfa_file->stop_program) {
  std::cout << "Error opening file." << std::endl;
  return 0;
}

my output:

Error opening file.

I know the file is open because in my parsing cpp file, i changed dfa_file.fail() to !dfa_file.is_open() and it stopped giving me that error. so what am i doing wrong?

EDIT: I was able to fix the problem by putting the file's full path in variable filename. so like in my main.cpp file i have

std::string filename = "/Users/myname/Documents/DFA/no000.dfa";
6
  • stop_program is undefined. Commented Feb 23, 2016 at 7:14
  • @n.m. it actually is defined i just forgot to write it down on here. sorry! Commented Feb 23, 2016 at 13:16
  • @astrocat1997: Can you expand your example so it can be used to reliably reproduce the behaviour you observed, both the fail() and the ! is_open()? Commented Feb 23, 2016 at 14:43
  • @DevSolar what more do you need? sorry im new to c++ so im not sure what else i should put. Commented Feb 23, 2016 at 19:48
  • Write a minimum and complete C++ code that reproduces the issue and can be compiled. Your code is incomplete. Commented Feb 23, 2016 at 19:54

1 Answer 1

4

Initialize stop_program in the constructor.

Sign up to request clarification or add additional context in comments.

1 Comment

sorry! i had it initialized but i just forgot to write it on here. it should be good now.

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.