Peter Lapisu
Peter Lapisu

Reputation: 20995

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

Upvotes: 1

Views: 1657

Answers (1)

Yuji
Yuji

Reputation: 34185

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.

Upvotes: 5

Related Questions