由于只是个小实验,所以代码也不怎么规范,同时也可能存在一些问题,但对于实现创建多个文件夹这一功能还是没问题的,大伙也可以看下并做些修改,有不足的地方希望指出:
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
void main()
{
for (int i = 0; i < 5;i++)
{
string path;
char paths[15] = " " ;
path = "mkdir \output"+to_string(i); //mkdir后需要加空格
for (int j = 0; j < path.size(); j++) //将string转换成char
{
paths[j] = path.c_str()[j];
//cout << paths[j];
}
//cout << endl;
system(paths);
}
system("pause");
}
用于MFC创建多级目录:(从https://blog.youkuaiyun.com/time2017/article/details/87734098拷贝的,可用,代码贴出来需要时用)
BOOL CreateMultiDirectory(CString strPath)
{
CString strSubPath;
CString strMsg;
int nCount = 0;
int nIndex = 0;
//通过“\”来分割路径,从而创建各级的目录。
do
{
nIndex = strPath.Find(_T("\\"), nIndex) + 1;
nCount++;
} while ((nIndex - 1) != -1);
nIndex = 0;
//检查,并创建目录
while ((nCount - 1) >= 0)
{
nIndex = strPath.Find(_T("\\"), nIndex) + 1;
if ((nIndex - 1) == -1)
strSubPath = strPath;
else
strSubPath = strPath.Left(nIndex);
if (!PathFileExists(strSubPath))// - 检查目录是否存在
{
if (!CreateDirectory(strSubPath, NULL))// -不存在则创建目录
{
strMsg.Format(_T("创建目录【%s】失败!"), strSubPath);
AfxMessageBox(strMsg, MB_OK);
return FALSE;
}
}
nCount--;
};
return TRUE;
}