[英]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: 抱歉,但有些标点符号:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.