您的位置:首页 > 编程语言 > C语言/C++

三种C/C++创建文件夹的方法

2017-04-23 22:56 295 查看
第一种:

调用MFC封装好的接口函数,主要会用到 PathIsDirectory //判断是否存在
::CreateDirectory //创建
例如:
#include "shlwapi.h"
#pragma comment(lib,"shlwapi.lib")
#include <afx.h>
CString path = "../../../STL/stl2";
if (!PathIsDirectory(path))
{
::CreateDirectory(path, 0);
}


第二种:
编写C/C++函数实现该功能

例如:

#include <io.h>
#include <direct.h>
#define PATH_DELIMITER '\\'
bool createDirectory(const std::string folder) {
std::string folder_builder;
std::string sub;
sub.reserve(folder.size());
for (auto it = folder.begin(); it != folder.end(); ++it)
{
//cout << *(folder.end()-1) << endl;
const char c = *it;
sub.push_back(c);
if (c == PATH_DELIMITER || it == folder.end() - 1)
{
folder_builder.append(sub);
if (0 != ::_access(folder_builder.c_str(), 0))
{
// this folder not exist
if (0 != ::_mkdir(folder_builder.c_str()))
{
// create failed
return false;
}
}
sub.clear();
}
}
return true;
}

const std::string path2 = "..\\..\\..\\STL\\stl2";
createDirectory(path2);


第三种:
调用DOS命令

例如:

#include <stdlib.h>
system("md stl2");


参考:https://github.com/liuruoze/EasyPR.git
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++C