0

I am writing a (hopefully) cross-platform application in C++. I need to create a directory. However it seems there is no standard interface in C++ to create a directory. A workaround I have come up with is to use the mkdir command. mkdir works in cmd, Powershell, and Bash. I am hoping that this will address most cross-platform use cases. The code would be something like:

#include <cstdlib>

int main(int argc, char** argv) {
    int errorCode = system("mkdir SOME_DIRECTORY");
    if (errorCode != 0) {
        // OOPS, SOMETHING WENT WRONG
    }
    else {
        // ALL'S WELL THAT ENDS WELL
    }
}

Is there a reason I should not take this approach? Are there other convenient alternatives for creating directories?

hazrmard
  • 3,397
  • 4
  • 22
  • 36
  • Have you considered `boost::filesystem` instead? – Dean Seo Apr 27 '17 at 03:36
  • *"Is there a reason I should not take this approach?"* You mean besides the inefficiency of spawning a new process for such a trivial task? – Alexander O'Mara Apr 27 '17 at 03:36
  • The application is quite lightweight. I am trying to have as few dependencies as possible. The operation only happens once in the code. So I am not concerned about performance that much. – hazrmard Apr 27 '17 at 03:38
  • There's a `std::filesystem` library in C++17 that's mostly the same as the boost version, but compilers don't support it quite yet. – Miles Budnek Apr 27 '17 at 04:04

0 Answers0