Re: Saving/Opening Files - different under PB than with build of app
Re: Saving/Opening Files - different under PB than with build of app
- Subject: Re: Saving/Opening Files - different under PB than with build of app
- From: Hampus Edvardsson <email@hidden>
- Date: Sun, 23 Dec 2001 16:15:49 +0100
On svndag, december 23, 2001, at 03:28 , Jim Correia wrote:
The working directory may not be the same under all situations. It
is "/" when the Finder launches you (on current builds of X). You
should compute the full path to the file and use that to open it.
If you only know the location of the file relative to your application
(Perhaps if you want to open a file in the same folder as the app), you
can use the fact that the path to your app is stored in the first
command line argument.
The following code will set the working directory to the parent
directory of the application:
(The code originally comes from the SDL sample project)
#import <sys/param.h> /* for MAXPATHLEN */
#import <unistd.h>
void setStartDirectory(const char* arg0) {
char parentdir[MAXPATHLEN];
char *c;
strncpy ( parentdir, arg0, MAXPATHLEN );
c = (char*) parentdir;
while (*c != '\0') /* go to end */
c++;
while (*c != '/') /* back up to parent */
c--;
*c = '\0'; /* cut off last part (binary name) */
assert ( chdir (parentdir) == 0 ); /* chdir to the binary app's
parent */
assert ( chdir ("../../../") == 0 ); /* chdir to the .app's parent */
}
int main(int argc, const char *argv[])
{
setStartDirectory(argv[0]);
return NSApplicationMain(argc, argv);
}
// Hampus Edvardsson