简体   繁体   中英

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM