我正在尝试在一个主要用 C++ 编写的 iOS 应用程序中使用 std::ifstream 读取存储在 Assets 文件夹及其子文件夹中的文件(相同的代码也用于其他非 iOS 项目),但它们是未找到。示例:有一个文件 assets/shaders/ortho2d.vert,我正在尝试像这样加载它:
std::ifstream vertFStream( vertFile ); // vertFile's contents is "assets/shaders/ortho2d.vert"
if (!vertFStream) {
std::cerr << vertFile << " missing!" << std::endl;
exit( 1 );
}
我已将 assets 文件夹作为蓝色文件夹添加到 XCode 项目中,它显示在 Targets->Copy Bundle Resources 中。
最佳答案
试试这个:
NSBundle *b = [NSBundle mainBundle];
NSString *dir = [b resourcePath];
NSArray *parts = [NSArray arrayWithObjects:
dir, @"assets", @"shaders", @"ortho2d.vert", (void *)nil];
NSString *path = [NSString pathWithComponents:parts];
const char *cpath = [path fileSystemRepresentation];
std::string vertFile(cpath);
std::ifstream vertFStream(vertFile);
关于iphone - 使用 C++ 在 iOS 应用程序中找不到的文件夹中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4313759/