View Full Version : Trouble with paths in Xcode
Mar 15, 2007, 12:25 PM
A program I've written in Xcode makes use of several files. When I run the program from inside Xcode, everything works fine. However, when I run the executable that's created from outside of Xcode, it doesn't find any of the input files.
Any ideas on how to fix this?
Mar 15, 2007, 12:32 PM
A few questions.
How are you referening the files? Full Paths? What language are you using?
Can you upload the source?
Mar 15, 2007, 12:40 PM
I'm using a combination of Objective C and C++, and I'm using relative paths to reference the files. The location of the executable is such that the reletive paths are correct.
It seems as though the program's using a different working directory when it's being run from Xcode than it does when being run as an executable. I just can't seem to figure out why that is.
Here's an example of the code, though I'm not sure this will help much.
string exampleParagraph = "./ComprehensionExamples/ExampleParagraph.txt";
ifstream paragraphFile(exampleParagraph.c_str(), ios::in);
Mar 15, 2007, 12:43 PM
There was another thread recently similar to this. I think it was the 'Xcode and Input Files' thread.
b e n
Mar 15, 2007, 12:50 PM
I'd recommend using the full path.
Find the path of the executable's folder, then add on the relative path to your input files.
Mar 15, 2007, 05:11 PM
In Xcode, the executable path is:
Project path->build->Debug(or Release)->application name
...which is also the current working directory.
But when you run the executable outside the working directory, the executable path is the same as the working directory. Many beginners miss the fact that when they click the "build and run" button the working directory IS NOT the project directory, but a location inside that (which I described above).
Does that help? You don't give any information as to where you have put these input files, and from where you run the final program.
Mar 15, 2007, 06:23 PM
It seems as though the program's using a different working directory when it's being run from Xcode than it does when being run as an executable. ...
You answered you own question: Xcode does a "cd".
It's really not a good design to have your program use a path relative to the current working directory. Maybe you can change it so it uses a path relative to the user's home directly. Just append the content of the $HOME environment variable to the relative path.
Mar 16, 2007, 12:34 AM
Can't you use NSBundle to help you access stuff within your own app bundle?
Mar 16, 2007, 03:25 AM
That would restrict the program to only being able to read data from within the resources folder of the app. While this is good for images, sound that a program uses, it isn't good if you need data that changes or need to read data from sources. If you need that then I believe it is called NSDefaults allows you to store information about where data should be read from and with the pref panel code you can write new defaults out that will allow the program to read from any directory the user chooses.
Mar 16, 2007, 03:46 AM
How does storing stuff in a prefs file help the OP with his problem. He can't access the file. Knowing how NSUserDefaults works isn't gonna help much when he can't access the file in the first place.
[NSBundle mainBundle] will return the path of the app. He can then build a path to anywhere in the app bundle from that - if that's where the files are (which seems to the case from his 2nd post). If it's not then use the tilde for the home directory and use NSString's stringByExpandingTildeInPath method.
NSString* fullPath=[@"~/Documents/MyStuff" stringByExpandingTildeInPath];
Mar 16, 2007, 04:47 AM
The only reason I mentioned the defaults is it allows the program to assume a good general location for where files will be loaded from.
Maybe the program will only ever been used by the OP, but if this is not the case, can you imagine most Mac users being told to put files in only a certain directory otherwise the app won't work?