PDA

View Full Version : SetCurrentDirectory() equivalent?


spiralmonkey
2007.02.28, 10:07 PM
This is definitely a newbie question - i'm in the process of porting an application from Windows and i can't seem to find the mac equivalent of the windows SetCurrentDirectory(path) command. Something that sets the default path when using ifstream. Can anyone enlighten me?

OneSadCookie
2007.02.28, 10:15 PM
man chdir

spaceb
2007.04.22, 01:36 AM
yes, use the unix chdir() function. There are 2 snags that got me the first time I tried this, so here are some tips:

1) add #include <unistd.h> to your file
2) I have OS 10.2 (an older version of OS 10 than most), so someone correct me if this has changed - The working directory, by default, is NOT the .app folder (ie, the application). It's actually 2 folders down (BLAH.app/Contents/MacOS), so keep this in mind when using chdir().

Good Luck!

akb825
2007.04.22, 01:54 AM
yes, use the unix chdir() function. There are 2 snags that got me the first time I tried this, so here are some tips:

1) add #include <unistd.h> to your file
2) I have OS 10.2 (an older version of OS 10 than most), so someone correct me if this has changed - The working directory, by default, is NOT the .app folder (ie, the application). It's actually 2 folders down (BLAH.app/Contents/MacOS), so keep this in mind when using chdir().

Good Luck!
I don't know if they ever changed it, but at least right now the default working directory is root. (if you launch a debug build of your app through XCode, though, it changes your working directory. I don't remember the exact location it changes it to, though)

spaceb
2007.04.22, 03:26 AM
hmm - Are you talking about the Mac file system's default directory or the UNIX file system's? I ask because the UNIX default directory is the one that needs to be changed to fix the problem, and this is the directory that will be affected by chdir().

Some extra info for the original poster:

If you're using standard library functions for files (fopen(), fstream, etc.), then you're using a different default (ie, working) directory than the Mac OS file functions do - chdir(), which changes the UNIX working dir but not the Mac one, will only affect the standard C/C++ functions like fopen().

This caused me plenty of headaches before I knew how to fix it, because I kept using the Mac SetWorkingDirectory function (or whatever it's called), and it wasn't affecting fopen.

OneSadCookie
2007.04.22, 05:39 AM
The default working directory for a process is the working directory of its parent process...

In the case of a Finder launch, it's /, but AFAIK that could change at any moment. In the case of an Xcode launch, it's the directory containing the .app. You should not make any assumptions.