简体   繁体   English

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

[英]Files in folders not found in iOS app using C++

I'm trying to read files stored in assets folder and its subfolders using std::ifstream in an iOS app written mostly in C++ (The same code is also used in other, non-iOS projects), but they're not found. 我正在尝试使用std :: ifstream在主要使用C ++编写的iOS应用程序中读取存储在assets文件夹及其子文件夹中的文件(相同的代码也用于其他非iOS项目),但是找不到它们。 Example: there is a file assets/shaders/ortho2d.vert and I'm trying to load it like this: 示例:有一个文件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 );
}

I've added the assets folder to the XCode project as a blue folder and it shows up in Targets->Copy Bundle Resources. 我已将assets文件夹作为蓝色文件夹添加到XCode项目中,它显示在Targets-> Copy Bundle Resources中。

Try this: 尝试这个:

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

You may need to check the relative path from where the application is running and probably use a full path to ensure the file is found. 您可能需要检查应用程序运行位置的相对路径,并可能使用完整路径来确保找到该文件。

The fact that the open failed does not necessarily mean the file is not found, it just might not be readable at this moment. 打开失败的事实并不一定意味着找不到文件,此时它可能无法读取。 (Incorrect permissions or file locked). (权限不正确或文件已锁定)。

exit(1) is rather drastic. 退出(1)是相当激烈的。

sorry but some punctuations: 抱歉,但有些标点符号:

  1. on iOS using file system calls from C++ is highly discouraged for security issues and limited support from a security point of view. 在iOS上使用来自C ++的文件系统调用非常不鼓励安全问题和从安全角度来看的有限支持。 calls to file system should be done afer you know decently iOS app 调用文件系统应该在你知道正确的iOS应用程序之后完成
    folder layout. 文件夹布局。 (bundles, resources, Documents folder" and so on..) otherwise it will fail. c) you can mix c++ and objC but definitively is not a correct approach. (捆绑,资源,文档文件夹等等)否则会失败.c)你可以混合使用c ++和objC,但最终不是一种正确的方法。
  2. under iOS you must use swift or objC (excect in very limited cases) 在iOS下你必须使用swift或objC(在非常有限的情况下除外)
  3. use iOS APIs, exactly as under android you would use java 使用iOS API,就像在android下使用java一样

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM