Xcode is loading program resources from my home folder, not resources - C ++
This is my first time using Xcode and it is terrible for me how completely non-intuitive this IDE is. I've heard it has been better in the past and I really hope it has.
My problem is the resources my program is loading, the data file and the .ini file, it automatically looks for these files in my home folder. I want it to look for these files in the Resources folder .app. I am using C ++ and all the examples I found are for Objective-C.
Any idea on how to fix this?
source to share
You are probably assuming that when your application starts up, the current working directory of the process is your application package. This is not true. (Nothing to do with Xcode - this is how OS X works.)
Typically you would use NSBundle
(Objective-C) or CFBundle
(C) to find resources in your application. Since you are using C ++ use the C API.
To find the URL for the file "myFile.ini" in the Resources directory in your application:
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef url = CFBundleCopyResourceURL(mainBundle, CFSTR("myFile"), CFSTR("ini"), NULL);
UInt8 filePath[PATH_MAX];
if (CFURLGetFileSystemRepresentation(url, true, filePath, sizeof(filePath)))
{
// use your API of choice to open and read the file at filePath
}
Or just change CWD to your app bundle:
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef url = CFBundleCopyBundleURL(mainBundle);
UInt8 bundlePath[PATH_MAX];
if (CFURLGetFileSystemRepresentation(url, true, bundlePath, sizeof(bundlePath)))
{
if (chdir((const char*)bundlePath) == 0)
{
// now the CWD is your app bundle, and you can use relative path names to access files inside it
}
}
source to share