2014-11-04 77 views
0

我已經構建了一個自定義框架和隨附的資源包,用於其他項目。資源束包含各種.sqlite和.bin文件。我試圖用我的框架在另一個項目中打開一個.bin文件,但沒有成功。使用fopen打開.bundle中的文件()

假設我的軟件包名爲CustomFramework.bundle。我有一個類ResourceHelper.cpp在我的框架內試圖打開mybin.bin位於CustomFramework.bundle

這裏是我目前如何嘗試打開它:

void ResourceHelper::openBinFromResourceFolder(FILE **file) { 
    std::string path; 
    path = "CustomFramework.bundle/"; 
    path.append("mybin.bin"); 
    *file = fopen(path.c_str(), "rb"); 
} 

file這是fopen()調用後NULL

我想如何在我的.bundle中打開.bin文件?

感謝

回答

1

原來你必須讓主束繩和資源包追加到該字符串。這是我如何運作的。

void ResourceHelper::openBinFromResourceFolder(const char *binName, FILE **file) { 
    std::string path; 
    // split bin name into name and file type (bin) 
    std::string binStr = binName; 
    std::size_t pos = binStr.find("."); 
    std::string filename = binStr.substr(0, pos); 
    std::string type = binStr.substr(pos+1); 

    // get bundle and CFStrings 
    CFBundleRef mainBundle = CFBundleGetMainBundle(); 
    CFStringRef cf_resource_path = CFStringCreateWithCString(NULL, resourcePath_.c_str(), kCFStringEncodingUTF8); 
    CFStringRef cf_filename = CFStringCreateWithCString(NULL, filename.c_str(), kCFStringEncodingUTF8); 
    CFStringRef cf_file_type = CFStringCreateWithCString(NULL, type.c_str(), kCFStringEncodingUTF8); 
    CFURLRef url_resource = CFBundleCopyResourceURL(mainBundle, cf_filename, cf_file_type, cf_resource_path); 
    CFStringRef urlString = CFURLCopyFileSystemPath(url_resource, kCFURLPOSIXPathStyle); 
    path = CFStringGetCStringPtr(urlString, kCFStringEncodingUTF8); 

    *file = fopen(path.c_str(), "rb"); 
} 
相關問題