boost文件系统

//最近在写个文件传输的小东西,用到了一点boost的文件系统,虽然比较少,还是记录下,方便之后查找。

//注:大部分是抄袭网上的。http://wenku.baidu.com/view/527dfc7da26925c52cc5bfc7.html

//注:使用的boost库是1.51版本,

//注:其他的主要函数参考http://www.fengfly.com/plus/view-34986-5.html和http://blog.csdn.net/tang08/article/details/5775086


#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>

//遍历目录下的所有文件

int GetAllFileOfPath(string path)

{

    namespace fs = boost::filesystem;

    if (path.size() < 2)     //此处是原作者的,可能是绝对路径,最短的也是D:这样的最少也得有2。相对路径也可以,比如输入1,就是当前目录下的1。故,次处代码删除掉
    {
        cout <<"这玩意不是个目录"<<endl;
        return 0;
    }

    fs::path full_path(fs::initial_path());   //初始化为工作目录
    //cout<<full_path.generic_string()<<endl;
    //cout<<"相对路径"<<full_path.relative_path()<<endl;
    full_path = fs::system_complete(fs::path(path, fs::native)); //转换为绝对路径

/* 上面是获取绝对路径。

    例如工作目录是 E::\\workspace

    path 传参是 projects\\test

    那么full_path这个路径指的就是  E::\\workspace\\projects\\test


   也可以构造相对路径 例如 fs::path part_path(path);   那么这个part_path就是 projects\\test,这个是相对路径。

*/


    unsigned long file_count = 0;
    unsigned long dir_count = 0;
    unsigned long err_count = 0;

    if ( !fs::exists(full_path))   //检测该路径是否存在,可以是文件夹也可以是文件
    {
        cout<<"找不到配置文件目录,请检查目录是否存在:";
        cout<< full_path.generic_string()<<endl;   //打印出string

        return -1;
    }

    //遍历配置文件所在的文件夹,得到所有有配置文件名。
    if (fs::is_directory(full_path))   //是否是目录,即是否是文件夹
    {
        fs::directory_iterator end_iter;
        for (fs::directory_iterator dir_iter(full_path);
             dir_iter != end_iter;
             ++dir_iter)
        {
            try
            {
                if (fs::is_directory(*dir_iter))
                {
                    std::string strSurDir(full_path.generic_string());
                    strSurDir.append("\\");
                    strSurDir.append(dir_iter->path().leaf().generic_string());

                   /*leaf()获取的是当前的文件夹名,或者文件名。与之相对的是 branch_path

                    例如:full_path 为 E:\\1\\2    那么leaf()就是2,branch_path()就是E:\\1

                    我使用的时候,有个小问题。

                    如果路径(该函数的第一次入参path,递归调用时不算)是盘符的第一级文件夹时,如E:\\1时,branch_path()就是E:\\,leaf()就是1,

                    如果路径是不是盘符的第一级文件夹是,如E:\\1\\2是,branch_path()就是E:\\1,leaf()就是\\2,

                    不清楚是不是问题,也有可能是我处理逻辑写错了。*/


                    //如果有子目录,递归遍历
                    GetAllFileOfPath(strSurDir);
                }
                else
                {
                    std::string strFileName(full_path.generic_string());
                    strFileName.append("\\");
                    strFileName.append(dir_iter->path().leaf().generic_string());

                    fs::path full_file(fs::initial_path());
                    full_file = fs::system_complete(fs::path(strFileName, fs::native)); //绝对路径,貌似不需要也可以的。

                    //加载解析文件中的信息
                    //do something
                    cout<<full_file.generic_string()<<endl;
                }
            }
            catch (const std::exception & ex)
            {
                ++err_count;
                std::string strMsg = dir_iter->path().leaf().generic_string();
                strMsg.append(", 出现错误:");
                strMsg.append(ex.what());

                cout<<strMsg<<endl;
            }
        }
    }
    else
    {
        std::string strMsg = full_path.generic_string();
        strMsg.append(",不是文件目录。");

        cout<<strMsg<<endl;
        return -1;
    }

    return err_count;

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值