iphone - 使用 C++ 在 iOS 应用程序中找不到的文件夹中的文件

标签 iphone c++ xcode ios ifstream

我正在尝试在一个主要用 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/

相关文章:

objective-c - AuthorizationExecuteWithPrivileges 已弃用

ios - 使用xcode项目生成类图

iphone - 如何计算条目是在特定日期之前还是之后?

c++ - 如何为 OS X Yosemite 安装 Eclipse for C++?

iphone - UIWebview 本地化

c++ - const auto 和 auto const 如何应用于指针?

C++ 字符串数组搜索每个项目的输出

ios - 如何在xmpp的tableview中显示收到的消息

ios - CGAffineTransformMakeScale 动画不工作

iphone - 如何在谷歌搜索 IOS 应用程序中实现自动完成?