0

The best way to explain my problem is probably just to show you my code, because it's as simple as it gets.

#include <iostream>
#include <fstream>

int main (int argc, const char * argv[])
{

    std::ifstream in;
    std::string line;
    in.open("test.txt");
    if (in.fail()) std::cout << "failed. \n";
    getline(in, line);
    std::cout << line;

    return 0;
}

So when I run this, console is returning "failed." instead of opening up the file called test.txt — which is in the same folder as my .xcodeproj file and is also displayed in my Xcode navigator.

I'm not sure what I'm misunderstanding about this process, but I suspect it will be something simple.

Thanks! :)

Jarrod
  • 1,655
  • 2
  • 21
  • 35

1 Answers1

3

The file is in the same directory as your .xcodeproj file? Well, there's your problem right here.

By default, the working directory of a process launched from Xcode will be the output directory (that is, the directory where the program is). Depending on your Xcode version, it's probably going to be in <Project Directory>/build/Debug.

Try moving the file there.

Etienne de Martel
  • 34,692
  • 8
  • 91
  • 111
  • Woohoo! This was the solution, thanks! The folder name ended up being: /Users/username/Library/Developer/Xcode/DerivedData/project_dir/Build/Products/Debug – Jarrod Dec 23 '11 at 04:47
  • Found a good reference on creating output directories in Xcode 4: http://stackoverflow.com/questions/4233581/xcode-4-build-output-directory – Jarrod Dec 27 '11 at 05:35