赞助

C/C++创建多级目录

工作中没有小事:点石成金,滴水成河,只有认真对待自己所做的一切事情,才能克服万难,取得成功。

转载:https://blog.csdn.net/lenkco/article/details/123306898

转载:https://blog.51cto.com/u_15414551/4399691

 

#include <io.h>
#include <direct.h>
#include <string>

void ToUnixPath( std::string& s)
{
    std::string::size_type pos = 0;
    while ((pos = s.find('\\', pos)) != std::string::npos)
    {
        s.replace(pos, 1, "/");
        pos = pos + 2;
    }
}

void createMultiDir(const std::string& strPath)
{
    std::string path = strPath;
    ToUnixPath(path);

    if (path[path.length() - 1] != '/')
    {
        path += '/';
    }

    std::string::size_type pos = 0;
    std::string::size_type prevpos = 0;
    std::string tempStr(path);
    std::string strFolderPath;
    std::string stdFolderName;
    while ((pos = tempStr.find('/', pos)) != std::string::npos)
    {
        strFolderPath = tempStr.substr(0, pos);
        stdFolderName = tempStr.substr(prevpos, pos - prevpos);
        if (_access(strFolderPath.data(), 0) == 0)//目录存在
        {
            pos = pos + 1;
            prevpos = pos;
            continue;
        }
        _mkdir(strFolderPath.data());
        pos = pos + 1;
        prevpos = pos;
    }
}


int main()
{
    std::string path = "F:/dir/Folder/test/123";
    createMultiDir(path);
    getchar();
    return 0;
}

 

posted @ 2022-06-30 22:15  车臣  阅读(424)  评论(0编辑  收藏  举报