Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

NekoCode

macrumors newbie
Original poster
Jan 3, 2014
5
0
level.world
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.
 
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...
 
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.
 
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...

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.
 
Last edited:
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
 
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.
https://mikeash.com/getting_answers.html

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!
 
Oh, sorry, I just don't know what the problem itself. There are no any logs.

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.

And thanks for a tip about NSBundle! Now it's working!
I'm glad it worked out.


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
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.