Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用C在应用程序包(NSBundle)中找到文件的路径?_C_Macos_Core Foundation_Nsbundle - Fatal编程技术网

如何使用C在应用程序包(NSBundle)中找到文件的路径?

如何使用C在应用程序包(NSBundle)中找到文件的路径?,c,macos,core-foundation,nsbundle,C,Macos,Core Foundation,Nsbundle,是否有用于查找应用程序包中文件路径的C API 我知道这可以在Objective-C中使用以下语法完成 NSString *path = [[NSBundle mainBundle] pathForResource:@"MyImage" ofType:@"bmp"]; 是否有一个相应的函数,我可以从C或C++代码调用,也可以?< /P> < p>可以通过: CFBundleRef mainBundle = CFBundleGetMainBundle(); 详情如下: 之后,我能够使用以下代码

是否有用于查找应用程序包中文件路径的C API

我知道这可以在Objective-C中使用以下语法完成

NSString *path = [[NSBundle mainBundle] pathForResource:@"MyImage" ofType:@"bmp"];

是否有一个相应的函数,我可以从C或C++代码调用,也可以?< /P> < p>可以通过:

CFBundleRef mainBundle = CFBundleGetMainBundle();
详情如下:

之后,我能够使用以下代码检索应用程序包中的文件路径

// Get a reference to the main bundle
CFBundleRef mainBundle = CFBundleGetMainBundle();

// Get a reference to the file's URL
CFURLRef imageURL = CFBundleCopyResourceURL(mainBundle, CFSTR("MyImage"), CFSTR("bmp"), NULL);

// Convert the URL reference into a string reference
CFStringRef imagePath = CFURLCopyFileSystemPath(imageURL, kCFURLPOSIXPathStyle);

// Get the system encoding method
CFStringEncoding encodingMethod = CFStringGetSystemEncoding();

// Convert the string reference into a C string
const char *path = CFStringGetCStringPtr(imagePath, encodingMethod);

fprintf(stderr, "File is located at %s\n", path);
这似乎比它需要的时间要长一点,但至少它能工作