0

I am writing an Objective-c++ application and I am trying to reach the applications Contents folder.

Let's pretend my apps name is MyApp.app. If I run my application from Xcode then I can reach the Contents folder like this:

std::fstream file;
file.open("MyApp.app/Contents/some_text_file.txt");
if(file.is_open()) {
...
}
else {
...
}

But If I run my built application without Xcode, then file.is_open() fails.

Silex
  • 2,583
  • 3
  • 35
  • 59

2 Answers2

3

[[NSBundle mainBundle] bundlePath] will give you the path of the MyApp.app directory. You can then add Contents to that path.

Alternatively, [[NSBundle mainBundle] resourcePath] will give you the path to the MyApp.app/Contents/Resources directory. You can then remove the subdirectory from that path; see Working with paths from [[NSBundle mainBundle] resourcePath]

Community
  • 1
  • 1
Kristopher Johnson
  • 81,409
  • 55
  • 245
  • 302
2

Use NSBundle class to get informations on your application bundle. [[NSBundle mainBundle] bundlePath] give you full path to MyApp.app.

Emmanuel
  • 2,897
  • 1
  • 14
  • 15