赞助

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 @   车臣  阅读(437)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决
历史上的今天:
2016-06-30 使用VS2010开发Qt程序的一点经验(转载)
点击右上角即可分享
微信分享提示