#ifdef win32#include<io.h>#include<direct.h>#else#include<unistd.h>#include<sys/stat.h>#endif#include<stdint.h>#include<string>#defineMax_path_len 256#ifdef WIN32#defineACCESS (Filename,accessmode) _access (Filename,accessmode)#defineMKDIR (path) _mkdir (path)#else#defineAccess (Filename,accessmode) access (Filename,accessmode)#defineMKDIR (Path) MKDIR (Path,s_irwxu | S_irwxg | S_iroth | S_ixoth)#endif//from left to right, determine whether a folder exists, create it without//Example:/home/root/mkdir/1/2/3/4///Note: If the last one is a folder, you need to add ' \ ' or '/'int32_t CreateDirectory (ConstSTD::string&DirectoryPath) {uint32_t Dirpathlen=directorypath.length (); if(Dirpathlen >Max_path_len) { return-1; } CharTmpdirpath[max_path_len] = {0 }; for(Uint32_t i =0; i < Dirpathlen; ++i) {Tmpdirpath[i]=Directorypath[i]; if(Tmpdirpath[i] = ='\\'|| Tmpdirpath[i] = ='/') { if(ACCESS (Tmpdirpath,0) !=0) {int32_t ret=MKDIR (Tmpdirpath); if(Ret! =0) { returnret; } } } } return 0;} int32_t Main (int32_t argc,Char*argv[]) { if(ARGC = =2) { returnCreateDirectory (argv[1]); } return 0;}
C + + Create folders and subfolders