环境: Win7 x64,VS2015
目标: 创建文件夹
说明:_mkdir 创建文件夹有个限制条件,例如创建 C:\Test\Son,如果C:\Test文件夹不存在,则Son文件夹无法创建
代码:
#include <iostream>
#include <string>
#include<io.h>
#include <direct.h>
using namespace std;
void CreateFolder(const std::string &directoryPath)
{
char* fileName = const_cast<char*>(directoryPath.c_str());
char* tag;
for (tag = fileName; *tag; tag++)
{
if (*tag == '\\')
{
char buf[1024], path[1024];
strcpy_s(buf, fileName);
buf[strlen(fileName) - strlen(tag) + 1] = NULL;
strcpy_s(path, buf);
if (_access(path, 6) == -1)
{
_mkdir(path);
}
}
}
}
int main()
{
//路径必须以\\结尾
std::string dir = "C:\\Users\\Administrator\\AppData\\Local\\Temp\\中国 PR-88_Admi371020F41iv1\\";
CreateFolder(dir);
}
注意:路径参数必须以 \\ 结尾 !!!