SetCurrentDirectory() equivalent?

Nibbie
Posts: 3
Joined: 2011.02
Post: #1
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?
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
man chdir
Quote this message in a reply
spaceb
Unregistered
 
Post: #3
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!
Quote this message in a reply
Moderator
Posts: 1,140
Joined: 2005.07
Post: #4
spaceb Wrote: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)
Quote this message in a reply
spaceb
Unregistered
 
Post: #5
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.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #6
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.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Game Center/Openfeint equivalent for Mac mlilback 1 3,961 Nov 30, 2010 01:49 PM
Last Post: EvolPenguin
  OS X equivalent of ODE anthony 7 4,403 Mar 10, 2007 01:31 PM
Last Post: ChrisD
  Classic to Cocoa: Finding Equivalent Functionality Emehr 9 5,195 Feb 25, 2007 06:59 AM
Last Post: Emehr
  Mac Equivalent of MessageBox Puzzler183 5 9,518 Feb 5, 2005 08:20 PM
Last Post: Puzzler183