User Name
Password
AppleNova Forums » Programmer's Nook »

C++ in XCode -- relative paths?


Register Members List Calendar Search FAQ Posting Guidelines
C++ in XCode -- relative paths?
Thread Tools
Wyatt
On twitter: @bwyatt
 
Join Date: Mar 2005
Location: Near Indianapolis
 
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:
Code:
infile.open("/Users/myusername/Desktop/folder/file.dat");
I'd much rather type:
Code:
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.

Twitter: bwyatt | Minecraft: bwyatt_IN
  quote
thuh Freak
Finally broke the seal
 
Join Date: May 2004
 
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:

Code:
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:

Code:
#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.
  quote
scratt
Veteran Member
 
Join Date: Jul 2004
Location: M-F: Thailand Weekends : F1 2010 - Various Tracks!
Send a message via Skype™ to 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...

'Remember, measure life by the moments that take your breath away, not by how many breaths you take'
Extreme Sports Cafe | ESC's blog | scratt's blog | @thescratt

Last edited by scratt : 2007-05-03 at 19:55.
  quote
Enki
Senior Member
 
Join Date: Nov 2004
 
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.
  quote
ShadowOfGed
Travels via TARDIS
 
Join Date: Aug 2005
Location: Earthsea
 
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!


Apparently I call the cops when I see people litter.
  quote
Posting Rules Navigation
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Post Reply

Forum Jump
Thread Tools
Similar Threads
Thread Thread Starter Forum Replies Last Post
X11, libs and getting an Xcode project to work! scratt Programmer's Nook 3 2006-12-12 20:21
Importing Code Warrior Project to XCode kate Programmer's Nook 0 2006-05-15 04:55
xcode - i didnt want universal binaries AsLan^ Programmer's Nook 15 2005-12-30 00:11
Xcode importing.. Good references.. scratt Programmer's Nook 0 2005-11-18 03:07
Creating a standalone exacutable with Xcode.. scratt Programmer's Nook 1 2005-11-15 22:17


« Previous Thread | Next Thread »

All times are GMT -5. The time now is 19:47.


Powered by vBulletin®
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.
Copyright ©2004 - 2021, AppleNova