8

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.

SurvivalMachine
  • 7,946
  • 15
  • 57
  • 87

3 Answers3

12

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);
Jeremy W. Sherman
  • 35,901
  • 5
  • 77
  • 111
  • 2
    What language is that? It's definitely not C++. – Ben Voigt Nov 30 '10 at 18:43
  • 1
    The language is Objective-C++, which allows you to freely mix Objective-C and C++ code. See Apple's ["Using C++ with Objective-C"](http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocCPlusPlus.html) for more information. – Jeremy W. Sherman Dec 01 '10 at 01:52
  • 1
    Thanks so much! Even over 5 years later you've saved me a lot of headaches. :) – Tim Vermeulen Jan 17 '16 at 15:59
1

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.

CashCow
  • 30,981
  • 5
  • 61
  • 92
-1

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
ingconti
  • 10,876
  • 3
  • 61
  • 48
  • You don't provide any factual reference pointing where "using *system calls* from C++" should be discouraged or indisputably a *wrong approach*. In fact the poster seems to know what he's doing, and what happens is that STL c++ streams internally use iOS Posix interface (iOS is still Darwin OS, which is unix like and exposes a pure C API). Objective C API is most likely built on top of the same Posix interface, with some limited exceptions possible. The marked answer show a perfectly clean example of using c++ code by marshaling utf-8 paths from Objective C `NSString`. – ceztko Mar 02 '20 at 08:20
  • I ma not discuss the theory of c/c++ calls. they work, of course..I was explaining why this approach is wrong on this platform. I read carefully all Apple docs , and avoiding "C" and "C++" ... calls is a "mantra". – ingconti Mar 02 '20 at 16:32
  • The mantra is probably for the sake of consistency within the platform which is fine, but many people has milions of lines of perfectly portable C/C++ code and rewriting the code to ObjC would just be the wrong choice. – ceztko Mar 02 '20 at 16:36