Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Fenrir

macrumors newbie
Original poster
Mar 15, 2007
5
0
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?
 

Eraserhead

macrumors G4
Nov 3, 2005
10,434
12,250
UK
A few questions.
How are you referening the files? Full Paths? What language are you using?
Can you upload the source?
 

Fenrir

macrumors newbie
Original poster
Mar 15, 2007
5
0
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.
Code:
string exampleParagraph = "./ComprehensionExamples/ExampleParagraph.txt";
ifstream paragraphFile(exampleParagraph.c_str(), ios::in);
 

Soulstorm

macrumors 68000
Feb 1, 2005
1,887
1
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.
 

ChrisA

macrumors G5
Jan 5, 2006
12,907
2,155
Redondo Beach, California
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.
 

garethlewis2

macrumors 6502
Dec 6, 2006
277
1
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.
 

caveman_uk

Guest
Feb 17, 2003
2,390
1
Hitchin, Herts, UK
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.
Code:
NSString* fullPath=[@"~/Documents/MyStuff" stringByExpandingTildeInPath];
 

garethlewis2

macrumors 6502
Dec 6, 2006
277
1
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?
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.