C ++ MAC OS X无法写入〜/ Library / Application Support / <appname> </appname>

时间:2011-08-09 08:45:55

标签: c++ file macos std

如果我将文件保存在.app软件包中,它会保存正常,但Apple建议将文件保存在应用程序支持中 下

~/Library/Application Support/appname

~/Library/Application Support/bundleid

我试过了两次,但我总是得到一个例外。我正在获得应用支持的路径,即

/Users/myname/Library/Application Support/com.company.appname/

/Users/myname/Library/Application Support/AppName/

com.company.appnameinfo.plist内正确指定,AppNameproduct AppName.app,因此路径似乎正确。

{
    FilepathProcessor::pathForFile(fpath, "menustate", ".sav", 
                                   PATH_TO_DESTINATION_SAVE_LOAD_FOLDER);

    std::ofstream file;
    file.exceptions(std::ofstream::eofbit | std::ofstream::failbit | 
                    std::ofstream::badbit);
    INFO_ARG("prepare to save to '%s'", fpath.c_str());

    try {
        file.open(fpath.c_str(), std::ios::out | std::ios::binary | 
                  std::ios::trunc);
        ERROR_IF_ARG(!file.is_open(), "couldnt open file for saving at '%s'",
                     fpath.c_str(), return);

        //will pass this point

        //exception happens by first write
        WRITE_INT_TO_BINFILE(file, episode);

        //...

    } 
    catch (std::ofstream::failure e) {
        ERROR_IF_ARG(true, "exception %s", e.what(), return);
    }
    file.close();
}

输出:

INFO : prepare to save to '/Users/myname/Library/Application
       Support/com.comapny.appname/menustate.sav' [CALLED BY : saveToFile]

ERROR! 
    Text : exception basic_ios::clear

1 个答案:

答案 0 :(得分:5)

自动为您创建目录。在保存文件之前,首先需要检查目录是否存在,如果不存在,则需要创建目录。