SurvivalMachine SurvivalMachine - 1 year ago 49
C++ Question

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.

Answer Source

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);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download