• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Saving/Opening Files - different under PB than with build of app
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Follow-Ups:
    • Re: Saving/Opening Files - different under PB than with build of app
      • From: Jim Correia <email@hidden>
References: 
 >Re: Saving/Opening Files - different under PB than with build of app (From: Jim Correia <email@hidden>)

  • Prev by Date: Re: Succeeded?
  • Next by Date: Re: Saving/Opening Files - different under PB than with build of app
  • Previous by thread: Re: Saving/Opening Files - different under PB than with build of app
  • Next by thread: Re: Saving/Opening Files - different under PB than with build of app
  • Index(es):
    • Date
    • Thread