PDA

View Full Version : C++ in XCode -- relative paths?


Wyatt
2007-05-03, 14:25
I'm really sick of having to type out absolute paths to files in my C++ programs and then having to delete them when I send them to my professor, who uses Windows (Visual Studio wants relative paths).

Is there any way I can force my programs to use relative paths?

Here's what I'm having to type now:
infile.open("/Users/myusername/Desktop/folder/file.dat");

I'd much rather type:
infile.open("file.dat");

This is really the only problem I have in this class, and I just spent the last 30 minutes trying to figure out why my program wasn't writing to a file when the only problem was a mistake in the file path. (Yeah, that sounds stupid, but I'm running on about an hour of sleep today.)

Is there a quick line of code I can plug in? Or, even better, a preference in XCode that I'm missing? Ideally, I'm looking for a preference in XCode, but I'd settle for a preprocessor directive or a couple quick and dirty lines of code I can plug in somewhere.

thuh Freak
2007-05-03, 16:38
`relative paths` are relative to the current "working directory" of the application. in most *nix systems, you can find that with the PWD env variable. Theres no special thing to add for relative paths to work; it should be part of the standard libraries. The trick is knowing what your wd is. If you're good at terminal, you can run the program yourself:

cd /Users/myusername/Desktop/folder/
echo $PWD
./programName

the echo line proves that your working directory (ie, current folder) is what it is. if your files aren't opening right (or aren't saving anywhere reasonable), write this into your programs before your open/save calls:

#include <stdio.h> //put these with the rest of your inclues
#include <stdlib.h>
//...
// put the following right before your relative path attempts
fprintf( stderr, "cwd: %s\n", getenv("PWD") );

theres probably a better way to get the wd, but thats the first off my head. any attempt at a relative path after that call will be relative to what is printed on stderr.

i dont know from what directory xcode runs the programs, but that fprintf line should tell you.

scratt
2007-05-03, 19:28
Just create a Resources folder in your project, put anything you want to have as data in that, and then in your target make a 'Copy Files' build stage, and then set it to copy those files to the 'Resources' folder in your target. You need to then grab the items in your 'Resources' folder with your source, and physically copy them to the 'Copy File' build stage in your target. This is all inside Xcode. Then every time you compile the program these files will be made part of the final package. You will see a 'Copy' files phase, or something like that at the end of the build sequence.

If you go down the Xcode project file tree you will find your executable in one of the final folders, most likely 'Developement'.

If you 'Show Package Contents' you will see inside your target, inside the 'Contents' folder you will have a few folders like MacOS, a Frameworks folder, Resources folder etc.

Anything in those folders can simply be loaded as if it's in the same folder as your executable. Whether is is frameworks, data, plugins or whatever is all dealt with by your package, and by Xcode at build time, if you set it up properly in Xcode. There is a handy video somewhere online talking you through this.. I will try to find it, but don't have time today. PM me is you want it.

Obviously your 'data' should be in the 'Resources' folder you have made, not Frameworks (which is where your framworks for that project are stored), or MacOS (which is where your executable is really stored).

Hope that makes sense...

Enki
2007-05-05, 22:32
Or you can just put them the resources in the top level of your project folder and then:

1) highlight the executable file in the left hand frame
2) hit the info button in the top toolbar which brings up a pane with a number of options.
3) Select the general tab
4) and then the Project Directory radio button.

Anything put in the top level will act as if it was in the same directory as the executable if you double-clicked it to launch it. If you select custom directory you can do what Scratt mentions.

ShadowOfGed
2007-05-05, 22:41
In Xcode, it is possible to specify the current working directory of your program.

Select the "Edit Active Executable" from the "Project" menu. Then, set it to either "Project directory," which is the directory containing the .xcodeproj, or set it to a custom directory, and place your data files accordingly.

Hope that helps!

:)