1

I am trying to open these two files and read their contents into two different arrays, but whenever I try and open them I get the unable to open file dialog? I don't see anything incorrect but I am not a strong c++ user.

std::ifstream inFile; inFile.open("fives.txt"); if (inFile.is_open()) { while (! inFile.eof() ) { getline (inFile,line); fives[loop] = line; cout << fives[loop] << endl; loop++; } inFile.close(); } else cout << "Unable to open file"; inFile.open("search.txt"); loop=0; if (inFile.is_open()) { while (! inFile.eof() ) { getline (inFile,line); search[loop] = line; cout << search[loop] << endl; loop++; } inFile.close(); } else cout << "Unable to open file"; 
10
  • Which of your two if clauses is triggering the message? Commented Nov 1, 2011 at 22:49
  • 1
    Are you sure the file exists? If so, do you have read permission? Commented Nov 1, 2011 at 22:49
  • What do you mean by "open file dialog"? Commented Nov 1, 2011 at 22:50
  • 5
    do they exist in the current directory? Note, the "current directory" is the directory the program is executed from, but doesn't have to be the one the program file is saved in. Commented Nov 1, 2011 at 22:50
  • 4
    @Stratosja you just answered your own question. The files don't exist in the executable's working directory. Commented Nov 1, 2011 at 22:59

1 Answer 1

6

The files must exist in the current directory, where the current directory is the directory from which the program was executed (not necessarily the one where the executable is saved at).

In your case, you saved the files with the resources, not with the resulting binary (I'm guessing you're running from within the VC++, by default it sets the current directory to where the binary is stored), so the program cannot find them. Use either relative path to where the resources are, or copy the files you're looking for into the directory you're running from.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.