How to create a folder on a Mac with C++?

邮差的信 提交于 2019-12-20 05:27:06

问题


How do you have the user input the folder name and have it created in the desktop (for mac)? This is what I have so far.. (and extra code underneath)

#include <iostream>
#include <fstream>
#include <sys/stat.h>
#include <stdlib.h>
#include <stdio.h>
using namespace std;

int main ()
{
    char game_name [100];
        cout << "Game Name: ";
        cin >> game_name;

        const char* homeDir = getenv ("Home");
        char final [256];
        sprintf (final, "%s/Desktop/%s",homeDir, game_name);
        mkdir(final,0775);

other code.... .... ... ..

return 0;

}

回答1:


Environment variables are case sensitive, so you need to use getenv("HOME") instead of getenv("Home").




回答2:


Use Boost Library (though there will be overhead of setting up boost on your system but its worth for doing many other stuffs in C++): boost::filesystem::create_directories()

#include <boost/filesystem.hpp>

// your code....

boost::filesystem::create_directories("/bla/a");


来源:https://stackoverflow.com/questions/21816978/how-to-create-a-folder-on-a-mac-with-c

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