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

标签 c macos core-foundation nsbundle

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

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

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

是否也有相应的函数可以从 C 或 C++ 代码中调用?

最佳答案

Mike K pointed me in the right direction 之后,我能够使用以下代码检索应用程序包中文件的路径。

// 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);

这似乎比需要的要长一点,但至少它有效!

关于c - 如何使用 C 在应用程序包 (NSBundle) 中找到文件的路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8768217/

相关文章:

macos - Cocoa:获取某些文件类型的人类可读描述,仅知道扩展名

c - 编写 Linux shell 时管理标准输出/标准输入

c++ - GCC __builtin_ 函数

c - 在 c 中初始化指针的正确方法是什么?

macos - 如何在终端中使用 pushd 和 popd 命令?

macos - 在使用 install4j 创建的 Mac OS X 安装程序中更改 LSMinimumSystemVersion

swift - 创建 CFDictionary

c - 为什么 stat() 返回 EFAULT?

c - 从 C 测试 Shellcode - 总线错误 10

ios - 我如何调配 corefoundation 框架方法?