开发者

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 als开发者_开发问答o used in other, non-iOS projects), but they're not found. Example: there is a file assets/shaders/ortho2d.vert and I'm trying to load it like this:

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.


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.


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. calls to file system should be done afer you know decently iOS app
    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.
  2. under iOS you must use swift or objC (excect in very limited cases)
  3. use iOS APIs, exactly as under android you would use java
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜