getcwd won't work as it should

Discussion in 'Mac Programming' started by NekoCode, Jan 3, 2014.

  1. NekoCode macrumors newbie

    NekoCode

    Joined:
    Jan 3, 2014
    Location:
    level.world
    #1
    Hey guys,

    I have problem, when I am launching my application from xcode, it works fine and returns right working directory. But if I launch my app by clicking on .app file, it returns wrong working directory, so I got no files loaded.

    Here's code, it was working right in windows:
    Code:
    char *Sys_Cwd( void )
    {
    	static char cwd[1024];
        
    	getcwd( cwd, sizeof( cwd ) - 1 );
    	cwd[1024-1] = 0;
        
    	return cwd;
    }
    
    I am using Obj-C/C in one application. Debug path is like '/Users/nekocode/Library/Developer/Xcode/DerivedData/test-emfzvokvsxztayagktgyeonnvtuy/Build/Products/Debug' ( less than 512 )
    Thanks.
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    What makes you think it's the wrong directory when run as a .app? What, exactly, are you trying to do? Perhaps if you explain what you are trying to achieve the correct solution could be suggested. For example if you are trying to use cwd to load something from inside the application bundle that is almost certainly wrong...
     
  3. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #3
    A bundled .app doesn't have a specific working directory given to it when it's launched. You simply can't rely on the working directory being a specific location.

    If you assume the working directory is set to the app-bundle, then that's an assumption. What's your evidence (e.g. documentation) that this assumption is valid?

    If this behavior differs from Windows, then it differs. You shouldn't expect everything to be identical when writing for the different platforms. Doing so is a faulty assumption.


    There are NSBundle methods that can produce pathnames or URLs for any resources in the app bundle. Use them, instead of making assumptions about the working directory.
     
  4. NekoCode, Jan 3, 2014
    Last edited: Jan 3, 2014

    NekoCode thread starter macrumors newbie

    NekoCode

    Joined:
    Jan 3, 2014
    Location:
    level.world
    #4
    It doesn't load necessary files from current directory.

    And thanks, I'll try to do something with NSBundle.
    I just tried to
    Code:
    NSString *bundlePath = [[NSBundle mainBundle] resourcePath];
    , but it returns /*.app/Contents/Resource itself.
     
  5. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #5
    Please be specific. You say it's the wrong dir, but you haven't given any details.
    You expect the dir to be ______, but it's actually ______ when the program is launched by ______.
    The reason you expect the dir to be ______ is because the reference ______ says it should be.
    Fill in the blanks with actual data from running your actual program.

    Rules of Thumb:
    1. Describe what you expected to happen.
    2. Describe what actually happened.
    3. Be specific.
    4. Post your code (complete, including logging or debug, not just fragments of code).
    https://mikeash.com/getting_answers.html
     
  6. NekoCode thread starter macrumors newbie

    NekoCode

    Joined:
    Jan 3, 2014
    Location:
    level.world
    #6
    Oh, sorry, I just don't know what the problem itself. There are no any logs.
    And thanks for a tip about NSBundle! Now it's working!
    Code:
    NSString *bundlePath = [[NSBundle mainBundle] resourcePath];
        NSString *secondParentPath = [[bundlePath stringByDeletingLastPathComponent] stringByDeletingLastPathComponent];
        NSString *thirdParentPath = [[secondParentPath stringByDeletingLastPathComponent] stringByDeletingLastPathComponent];
        NSString *s = thirdParentPath;
        const char *c = [s UTF8String];
       // go to c-part now
    
    Thank you very much!
     
  7. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #7
    If there are no logs, then you need to tell us exactly how you're finding out what the value is. If you're using the debugger and stepping through lines, you need to tell us that. Details are important.

    I'm glad it worked out.


    If you intend to use the variable c as a filename or pathname for subsequent C functions, don't use UTF8String. Use the fileSystemRepresentation method instead.

    https://developer.apple.com/library...lasses/NSString_Class/Reference/NSString.html
    (See the section titled "Working with Paths")

    https://developer.apple.com/library...ptual/strings/Articles/ManipulatingPaths.html

    You might also want to look for an NSBundle method that returns the actual bundle path, rather than starting from resourcePath and stripping off components.
     
  8. NekoCode thread starter macrumors newbie

    NekoCode

    Joined:
    Jan 3, 2014
    Location:
    level.world

Share This Page