1

I've been trying to figure out how to set the working directory to the same directory as my program. As I am unfamiliar with Objective-C and Cocoa, I wrote this as a basic command line application, meaning standard in/out. For some reason, the working directory is set to the $HOME directory as opposed to the same directory as the file itself. I know using

system("PWD");

would echo the path, but in an unusable (at least to me) format.

Edit: Just for a little clarification, I need to be able to use relative file paths for file streams on more than just my machine/user.

Daniel
  • 23,129
  • 12
  • 109
  • 154
Stephen S
  • 166
  • 1
  • 1
  • 12
  • 1
    There is no such thing like "c++ working directory". Please specify what you meant. Is ist working directory for XCode? – klm123 Aug 12 '12 at 23:44
  • C++ does use a working directory for relative file paths. In a windows machine, the working directory is the directory the program is in. For OSX apparently, it's different. It's using my $HOME directory as the directory. – Stephen S Aug 12 '12 at 23:57

1 Answers1

2

You can set the directory in which your tool will start when run from Xcode in the scheme editor. In the toolbar, click on the name of your project (to the left of the >), then click "Edit Scheme…", click on "Run" in the list on the left side, then the "Options" tab on the right, and you'll see an option to use a custom working directory.

Note that you aren't guaranteed to be on a case-insensitive file system, so you should use a lowercase pwd or your system(3) call will fail. iOS never uses a case-insensitive filesystem, and OS X may be installed on either a case-insensitive or case-sensitive file system. getcwd(3) is more efficient/appropriate in any case.

Also, you should upgrade to a current version of Xcode if you can. The early 4.x versions were very rough around the edges (some would say 4.4/4.5 still are, but they're at least more stable).

Nicholas Riley
  • 43,532
  • 6
  • 101
  • 124
  • The problem with setting the working directory from Xcode is that I can only use the program on my computer on my user. If I were to use a different computer or a different user (which is my intention), it would and does fail. As for system("pwd"), I normally type uppercase for system calls because when I submit programs for instructors, they require uppercase system calls for windows machines. So it's mainly habit for uppercase calls. This app isn't an assignment though. As for upgrading Xcode, I intend to to that when I go back to college because my internet at home is absolutely horrid. – Stephen S Aug 12 '12 at 23:53
  • @Morph: The working directory is the directory from which the application is launched. That is not part of the binary, but rather the environment. If you open a terminal, change the directory to where the binary is and launch the application from there, that will be your directory. In general, though in unix systems applications will be launched (if launched from a graphical environment) from the user home directory. If you need, you can find the executable directory: [http://stackoverflow.com/questions/4087328/how-to-find-the-residing-directory-of-a-os-x-application-package-programmatica] – David Rodríguez - dribeas Aug 13 '12 at 01:42