简体   繁体   中英

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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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