VansFannel
VansFannel

Reputation: 45931

Reading a file from a C++ class used in an iOS project

I'm developing an iOS application with latest SDK and I'm also using OpenCV.

I have some C++ classes and I have to read the content of a XML with these C++ classes.

With Objective-C I do it in this way:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"MyFile" ofType:@"xml"];  
NSData *myData = [NSData dataWithContentsOfFile:filePath];  
if (myData) {  
    // do something useful  
}

But, how can I do it from C++?

I know that cpp need a file path and I don't know how can I get that path from C++.

All my OpenCV algorithms are in C++ classes, so I have to do it in those C++ classes.

Upvotes: 0

Views: 1599

Answers (2)

John
John

Reputation: 4092

You have two options:

  1. Open file in C++ using C string ([filePath fileSystemRepresentation] returns const char*)
  2. Open file in Objective C and then pass pointer to raw bytes and size ([myData bytes] and [myData length])

Upvotes: 1

trojanfoe
trojanfoe

Reputation: 122391

While it's, of course, possible to open the XML file and parse it using pure C++, I think you should use Apples-provided Objective-C classes to perform this parsing. You can do this by using Objective-C++ (i.e. mixed Objective-C and C++) simply by renaming your C++ implementation file extension from .cpp to .mm.

The reason I think this is a good idea:

  1. It's quicker to implement and less error-prone.
  2. If changes are made to the plist format, you will get those changes for free.

Upvotes: 1

Related Questions