1.使用direct.h头文中的_mkdir函数
#include <direct.h>
#include <iostream>
using namespace std;
int main()
{
string folderPath = "E:\\test";
if (0 != _mkdir(folderPath.c_str()))
{
// 返回 0 表示创建成功,-1 表示失败
_mkdir(folderPath.c_str());
}
return 0;
}
2.使用direct.h里的_mkdir函数和io.h里的_access函数
#include <direct.h>
#include <iostream>
#include <io.h>
using namespace std;
int main()
{
string folderPath = "E:\\test";
if (0 != _access(folderPath.c_str(), 0))
{
// 返回 0 表示创建成功,-1 表示失败
_mkdir(folderPath.c_str());
}
return 0;
}
3.使用system()调用dos命令
#include <iostream>
using namespace std;
int main()
{
string folderPath = "E:\\test";
string command;
command = "mkdir test " + folderPath;
system(command.c_str());
return 0;
}
4.使用Windows API函数
#include <windows.h>
#include <iostream>
using namespace std;
int main()
{
string folderPath = "E:\\test1";
if (!GetFileAttributes(folderPath.c_str()) & FILE_ATTRIBUTE_DIRECTORY)
{
bool flag = CreateDirectory(folderPath.c_str(), NULL);
// flag 为 true 说明创建成功
}
else {
cout << "Directory already exists." << endl;
}
return 0;
}