2

File opens if I write the full path (full-path/roots.txt). File fails to open if I write the filename only (roots.txt)

And yet, roots.txt is in the same folder as the main.cpp. Is there any settings I should check on XCode?

Here's the code:

    string line;
ifstream infile;
infile.clear();
// infile.open("roots.txt");
infile.open("/Users/programming/C++/roots/roots.txt");
if (infile.fail()) cout << "could not open the file: " << strerror(errno);
getline(infile, line);
cout << line;
tshepang
  • 12,111
  • 21
  • 91
  • 136
JohnG
  • 2,109
  • 3
  • 16
  • 12
  • 3
    Read up on Working Directories. http://stackoverflow.com/questions/3396378/change-the-working-directory-in-xcode – wkl Jan 25 '11 at 17:43

4 Answers4

9

By default, Xcode working directory is something like ~/Library/Developer/Xcode/DerivedData/project-/Build/Products/Debug. So,if you are trying to use a relative path with respect your project directory or any other, you should manually configure your working directory in Xcode.

You can do this by: Product -> Scheme -> Edit Scheme -> options tab, tick the use custom working directory checkbox and show your path.

Java Devil
  • 10,629
  • 7
  • 33
  • 48
Kaje
  • 851
  • 7
  • 18
4

If it works when you attempt to open a file with an absolute path and fails with just the filename, your relative path is likely incorrect. Ensure roots.txt is placed in the current working directory. Look into the getcwd function declared in unistd.h.

James
  • 5,355
  • 2
  • 18
  • 30
  • thanks! the relative path was in build/debug/. I am wondering how to change that in Xcode settings. – JohnG Jan 25 '11 at 19:28
4

To change the working directory when you're running from inside XCode: select "Edit Active Executable" in your "Project" menu.

You can adjust your working directory settings at the bottom of the "General" section.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82
  • Thanks for the tip. I changed Edit Active Executable "main" to Project directory (where roots.txt is). Unfortunately I still have the error "Not such file or directory." getcwd still gives me "/Users/programming/C++/roots/build/Debug" – JohnG Jan 25 '11 at 20:13
  • Are you launching your executable from inside XCode? – molbdnilo Jan 26 '11 at 05:21
  • If you're not launching from XCode, then that setting has no effect and the normal working directory rules apply. In Terminal, you should 'cd /Users/programming/c++/roots', then 'build/debug/main' (if your executable's name is "main"). – molbdnilo Jan 26 '11 at 05:41
  • Thanks. In build settings, I had to change: "Per-configuration Build Configuration Path" to "." instead of "build/debug" – JohnG Jan 26 '11 at 20:53
2

Assuming you're running file from within XCode, the working directory is unlikely to be the same directory where your .cpp file is located. Check what your current working directory is what you think it is. (you should be able to obtain it using a getcwd call)

Billy ONeal
  • 104,103
  • 58
  • 317
  • 552
  • Your answer was similar to mine and appeared before mine as I was typing. I was Ninja'd. – James Jan 25 '11 at 18:04