C++ MAC OS X cannot write to ~/Library/Application Support/<appname>

吃可爱长大的小学妹 提交于 2020-01-05 08:16:18

问题


If i save files inside the .app bundle, it saves OK, but apple recommends saving files inside the Application Support under

~/Library/Application Support/appname

or

~/Library/Application Support/bundleid

I tried both, but I am always getting an exception. I am getting a path to the Application Support, which is

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

or

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

com.company.appname is specified correctly inside my info.plist, and AppName is product AppName.app, so the paths seems correct.

{
    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();
}

Output :

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:


The directory is not automatically created for you. Before saving the file, you first need to check if the directory exists, and if not, you need to create the directory.



来源:https://stackoverflow.com/questions/6993527/c-mac-os-x-cannot-write-to-library-application-support-appname

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!