Create directory

    0

    0

    Giovanny Gongora

    Codiga's C++ Recipes

    Create a directory if it doesn't exist

    #include <sys/types.h>
    #include <sys/stat.h>
    #include <unistd.h>
    #include <string>
    
    int createDir(){
      struct stat st = {0};
      //checking if the directory exists
      if (stat("newdir", &st) == -1) {
        mkdir("newdir", 0755);
      }
      std::string mydir = "mydir";
      //need to convert from std::string to const char*
      if (stat(mydir.c_str(), &st) == -1) { //checking if the directory exists
        mkdir(mydir.c_str(), 0755);
      }
    }
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    Legal
    • Security
    • Privacy Policy
    • Code Privacy
    • Terms of Service
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.