Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 832
SurvivalMachine Avatar asked Nov 30 '10 12:11

SurvivalMachine


People also ask

How do I get iOS app files?

Select an app, then enter its Backup folder. Navigate that folder to find files. Select files you want to view; you may or may not be able to view them, depending on which apps are needed to read their data. Select files, then click Copy to Mac or Copy to PC to copy them to your computer.


1 Answers

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);
like image 79
Jeremy W. Sherman Avatar answered Sep 21 '22 09:09

Jeremy W. Sherman