2012-01-07 12 views
6

क्या एप्लिकेशन सी बंडल में फ़ाइल के पथ को खोजने के लिए कोई सी एपीआई है?सी का उपयोग कर एप्लिकेशन बंडल (एनएसबंडल) में फ़ाइल के पथ को मैं कैसे ढूंढ सकता हूं?

मुझे पता है कि यह निम्नलिखित वाक्यविन्यास के साथ उद्देश्य-सी में किया जा सकता है।

NSString *path = [[NSBundle mainBundle] pathForResource:@"MyImage" ofType:@"bmp"]; 

क्या कोई संबंधित फ़ंक्शन है जिसे मैं सी या सी ++ कोड से भी कॉल कर सकता हूं?

उत्तर

7

Mike K pointed me in the right direction के बाद, मैं के साथ एक आवेदन बंडल में एक फ़ाइल का पथ को पुनः प्राप्त करने में सक्षम था निम्नलिखित कोड।

// Get a reference to the main bundle 
CFBundleRef mainBundle = CFBundleGetMainBundle(); 

// Get a reference to the file's URL 
CFURLRef imageURL = CFBundleCopyResourceURL(mainBundle, CFSTR("MyImage"), CFSTR("bmp"), NULL); 

// Convert the URL reference into a string reference 
CFStringRef imagePath = CFURLCopyFileSystemPath(imageURL, kCFURLPOSIXPathStyle); 

// Get the system encoding method 
CFStringEncoding encodingMethod = CFStringGetSystemEncoding(); 

// Convert the string reference into a C string 
const char *path = CFStringGetCStringPtr(imagePath, encodingMethod); 

fprintf(stderr, "File is located at %s\n", path); 

ऐसा लगता है कि यह होने की अपेक्षा थोड़ा लंबा लगता है, लेकिन कम से कम यह काम करता है!