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

第一种:

调用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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值