1

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;

}
jackslash
  • 8,550
  • 45
  • 56
user3316925
  • 23
  • 2
  • 4

2 Answers2

1

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

dreamlax
  • 93,976
  • 29
  • 161
  • 209
1

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");
Ankit Singh
  • 2,602
  • 6
  • 32
  • 44
  • 1
    Boost is overkill for `mkdir()` alone. However, if you have a lot of these sort of system calls, boost will make it a lot easier to port to other operating systems. – Hal Canary Feb 16 '14 at 22:30
  • Yes. That is why I wrote its worth if he wants to do other cool stuffs in C++. – Ankit Singh Feb 17 '14 at 06:39