Advise analog CreateDirectoryA for Linux. Thank you in advance.

    1 answer 1

    #include <boost/filesystem.hpp> //... boost::filesystem::create_directories("/any/dir/a/b/c"); 

    In general, Linux has mkdir , and system () - which will allow you to do the following:

     system("mkdir -p /any/dir/a/b/c") 

    I took the code from enSO:

    The author claims that the code works on both a plunger and Linux:

     #include <iostream> #include <string> #include <sys/stat.h> // stat #include <errno.h> // errno, ENOENT, EEXIST #if defined(_WIN32) #include <direct.h> // _mkdir #endif bool isDirExist(const std::string& path) { #if defined(_WIN32) struct _stat info; if (_stat(path.c_str(), &info) != 0) { return false; } return (info.st_mode & _S_IFDIR) != 0; #else struct stat info; if (stat(path.c_str(), &info) != 0) { return false; } return (info.st_mode & S_IFDIR) != 0; #endif } bool makePath(const std::string& path) { #if defined(_WIN32) int ret = _mkdir(path.c_str()); #else mode_t mode = 0755; int ret = mkdir(path.c_str(), mode); #endif if (ret == 0) return true; switch (errno) { case ENOENT: // parent didn't exist, try to create it { int pos = path.find_last_of('/'); if (pos == std::string::npos) #if defined(_WIN32) pos = path.find_last_of('\\'); if (pos == std::string::npos) #endif return false; if (!makePath( path.substr(0, pos) )) return false; } // now, try to create again #if defined(_WIN32) return 0 == _mkdir(path.c_str()); #else return 0 == mkdir(path.c_str(), mode); #endif case EEXIST: // done! return isDirExist(path); default: return false; } } int main(int argc, char* ARGV[]) { for (int i=1; i<argc; i++) { std::cout << "creating " << ARGV[i] << " ... " << (makePath(ARGV[i]) ? "OK" : "failed") << std::endl; } return 0; } 

    Usage:

     $ makePath 1/2 folderA/folderB/folderC creating 1/2 ... OK creating folderA/folderB/folderC ... OK 

    Offtopic: I start to feel depressed when I see pogrommists who start to pogrommic on Windows (


    • And if creation with a variable? For example, as he wrote for Windows. std :: string kor = ("somedir /"); CreateDirectoryA (kor.c_str (), NULL); - Honey Cake
    • Calm, only calm :) - even on Windows, normal programmers calmly use _mkdir() . Well, and if a person uses the system("md...") for this, then all the same, under what operating system is this freak ... - Harry
    • and the correct answer was a simple mkdir(2) ... - Fat-Zer