Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code runs perfect in g++ but not in Xcode - Cannot find File

Tags:

c++

xcode

g++

I have created a text file with content. It is located in the same folder as the cpp files. And I have confirmed several times that the file exists. When I run g++, compile and run it finds the file. When I run it in Xcode, it does not work. If fails to find the file.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
  string line;
  ifstream myfile ("example.txt");
  if (myfile.is_open())
  {
    while ( myfile.good() )
    {
      getline (myfile,line);
      cout << line << endl;
    }
    myfile.close();
  }

  else cout << "Unable to open file"; 

  return 0;
}
like image 709
diek Avatar asked Jan 23 '13 09:01

diek


People also ask

How do I fix Xcode file not found?

I was facing this error after a merge from the repository (git). The final solution was: Delete the files from the xcode project navigator (only deleting references). Add them again to the project (dragging from finder into the project navigator).


1 Answers

Your file fails to open because XCode launches from the IDE in the default build location, which is actually a temporary directory off somewhere on your disk. If you want change the working directory at launch-time to something else (like the location where your files can be found):

  1. Select the Product/Edit Scheme... menu option.
  2. Select the Run schema in the left list.
  3. At the bottom Options tab on the right pane should be a "Working Directory" option. Check the checkbox and set the custom working directory to someplace you know (your "/Users/yourname" home directory is a decent place that I use).
  4. Make sure any "current directory" data files you need for your program execution from the IDE are in this directory.

And in case you didn't see it, this is also the place where you can configure command-line arguments (on another tab) of the same dialog.

like image 80
WhozCraig Avatar answered Oct 13 '22 10:10

WhozCraig