boost 文件操作


void testFileSystem()
{
    boost::filesystem::path path("/test/test1");   //初始化 
    boost::filesystem::path old_cpath = boost::filesystem::current_path(); //取得當前程序所在文件夾  
    boost::filesystem::path parent_path = old_cpath.parent_path();//取old_cpath的上一層父文件夾路徑 
    boost::filesystem::path file_path = old_cpath / "file"; //path支持重載/運算符
    std::string file_dir = file_path.string();
    if (boost::filesystem::exists(file_path))  //推斷文件存在性  
    {
        std::string strPath = file_path.string();
        int x = 1;
    }
    else
    {
        //文件夾不存在;   
        boost::filesystem::create_directory(file_path);  //文件夾不存在。創建 
    }
    bool bIsDirectory = boost::filesystem::is_directory(file_path); //推斷file_path是否為文件夾
    boost::filesystem::recursive_directory_iterator beg_iter(file_path);
    boost::filesystem::recursive_directory_iterator end_iter;
    for (; beg_iter != end_iter; ++beg_iter)
    {
        if (boost::filesystem::is_directory(*beg_iter))
        {
            continue;
        }
        else
        {
            std::string strPath = beg_iter->path().string();  //遍歷出來的文件名稱
            int x = 1;
        }
    }
    boost::filesystem::path new_file_path = file_path / "test.txt";
    if (boost::filesystem::is_regular_file(new_file_path))    //推斷是否為普通文件
    {
        UINT sizefile = boost::filesystem::file_size(new_file_path);  //文件大小(字節)
        int x = 1;
    }
    boost::filesystem::remove(new_file_path);//刪除文件new_file_path  
}

// recusively copy file or directory from $src to $dst 
void CopyFiles(const boost::filesystem::path &src, const boost::filesystem::path &dst)
{
    if (!boost::filesystem::exists(dst))
    {
        boost::filesystem::create_directories(dst);
    }
    for (boost::filesystem::directory_iterator it(src); it != boost::filesystem::directory_iterator(); ++it)
    {
        const boost::filesystem::path newSrc = src / it->path();
        const boost::filesystem::path newDst = dst / it->path();
        if (boost::filesystem::is_directory(newSrc))
        {
            CopyFiles(newSrc, newDst);
        }
        else if (boost::filesystem::is_regular_file(newSrc))
        {
            boost::filesystem::copy_file(newSrc, newDst, boost::filesystem::copy_option::overwrite_if_exists);
        }
        else
        {
            _ftprintf(stderr, _T("Error: unrecognized file - %s"), newSrc.string().c_str());
        }
    }
}

bool CopyDirectory(const std::string &strSourceDir, const std::string &strDestDir)
{
    boost::filesystem::recursive_directory_iterator end; //設置遍歷結束標志,用recursive_directory_iterator即可循環的遍歷目錄  
    boost::system::error_code ec;
    for (boost::filesystem::recursive_directory_iterator pos(strSourceDir); pos != end; ++pos)
    {
        //過濾掉目錄和子目錄為空的情況  
        if (boost::filesystem::is_directory(*pos))
            continue;
        std::string strAppPath = boost::filesystem::path(*pos).string();
        std::string strRestorePath;
        //replace_first_copy在algorithm/string頭文件中,在strAppPath中查找strSourceDir字符串,找到則用strDestDir替換,替換后的字符串保存在一個輸出迭代器中  
        boost::algorithm::replace_first_copy(std::back_inserter(strRestorePath), strAppPath, strSourceDir, strDestDir);
        if (!boost::filesystem::exists(boost::filesystem::path(strRestorePath).parent_path()))
        {
            boost::filesystem::create_directories(boost::filesystem::path(strRestorePath).parent_path(), ec);
        }
        boost::filesystem::copy_file(strAppPath, strRestorePath, boost::filesystem::copy_option::overwrite_if_exists, ec);
    }
    if (ec)
    {
        return false;
    }
    return true;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM