本篇文章給大家分享的是有關(guān)boost中如何使用filesystem庫,小編覺得挺實用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

目前創(chuàng)新互聯(lián)公司已為1000+的企業(yè)提供了網(wǎng)站建設(shè)、域名、雅安服務(wù)器托管、網(wǎng)站改版維護、企業(yè)網(wǎng)站設(shè)計、大渡口網(wǎng)站維護等服務(wù),公司將堅持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
ma_dir_op.h:
////////////////////////////////////////////////////////////////
//
//Descript: directory operation class
// Author: guowenyan
// Date: 2013.06.13
//
////////////////////////////////////////////////////////////////
#pragma once
#include <string>
#include <vector>
class CMaDirOperation
{
public:
static CMaDirOperation *get_instance();
public:
bool traverse_path(std::vector<std::string> &vec_file_name, const std::string &path, const std::string &file_type = "") const;
bool is_file_exist(const std::string &path, const std::string &file_name) const;
bool is_file_exist(const std::string &file_name) const;
bool delete_file(const std::string &path, const std::string &file_name) const;
bool delete_file(const std::string &file_name) const;
bool delete_directory(const std::string &path_name) const;
void copy_file(const std::string &s_file_name, const std::string &d_file_name) const;
void copy_directory(const std::string &s_path_name, const std::string &d_path_name) const;
bool chang_file_type(const std::string &file_name, const std::string &s_file_type, const std::string &d_file_type, std::string &changed_file_name) const;
private:
CMaDirOperation() { };
~CMaDirOperation() { };
bool is_file_type(const std::string &file_name, const std::string &file_type) const;
private:
static CMaDirOperation* m_p_instance;
};ma_dir_op.cpp:
////////////////////////////////////////////////////////////////
//
//Descript: directory operation class
// Author: guowenyan
// Date: 2013.06.13
//
////////////////////////////////////////////////////////////////
#include "ma_dir_op.h"
#include <iostream>
#include <boost/filesystem.hpp>
using namespace std;
namespace fs = boost::filesystem;
CMaDirOperation* CMaDirOperation::m_p_instance = NULL;
CMaDirOperation* CMaDirOperation::get_instance()
{
if(NULL == m_p_instance)
m_p_instance = new CMaDirOperation();
return m_p_instance;
}
bool CMaDirOperation::traverse_path(vector<string> &vec_file_name, const string &path, const string &file_type/* = ""*/) const
{
if(!fs::exists(path))
{
cout<<"the path "<<path.c_str()<<" is not exist."<<endl;
return false;
}
if(!fs::is_directory(path))
{
cout<<"the path "<<path.c_str()<<" is not a directory."<<endl;
return false;
}
fs::directory_iterator ite_begin(path);
fs::directory_iterator ite_end;
for(; ite_begin != ite_end; ite_begin++)
{
fs::path path_tmp(*ite_begin);
if(fs::is_regular_file(path_tmp))
{
path_tmp = path_tmp.filename();
string str = path_tmp.string();
if(is_file_type(str, file_type))
vec_file_name.push_back(str);
}
}
return true;
}
bool CMaDirOperation::is_file_exist(const string &path, const string &file_name) const
{
string t_path = path;
if( *(t_path.end()-1) != '/' )
{
t_path += '/';
}
return is_file_exist(t_path + file_name);;
}
bool CMaDirOperation::is_file_exist(const string &file_name) const
{
fs::path path_file(file_name);
if(!fs::exists(path_file))
{
return false;
}
return fs::is_regular_file(path_file);
}
bool CMaDirOperation::delete_file(const std::string &path, const std::string &file_name) const
{
string t_path = path;
if( *(t_path.end()-1) != '/' )
{
t_path += '/';
}
return delete_file(t_path + file_name);;
}
bool CMaDirOperation::delete_file(const std::string &file_name) const
{
/*fs::path path_file(file_name);
if(!fs::exists(path_file) || !fs::is_regular_file(path_file))
{
return false;
}
return fs::remove(path_file);*/
return true;
}
bool CMaDirOperation::delete_directory(const std::string &path_name) const
{
vector<string> vec_file_name;
if(!traverse_path(vec_file_name, path_name))
return false;
vector<string>::iterator ite = vec_file_name.begin();
for(; ite != vec_file_name.end(); ite++)
{
delete_file(path_name + (*ite));
}
return true;
}
void CMaDirOperation::copy_file(const string &s_file_name, const string &d_file_name) const
{
fs::path s_path(s_file_name);
fs::path d_path(d_file_name);
fs::copy_file(s_path, d_path, fs::copy_option::fail_if_exists);
}
void CMaDirOperation::copy_directory(const std::string &s_path_name, const std::string &d_path_name) const
{
vector<string> vec_file_name;
if(traverse_path(vec_file_name, s_path_name))
{
vector<string>::iterator ite = vec_file_name.begin();
for(; ite != vec_file_name.end(); ite++)
{
copy_file(s_path_name + (*ite), d_path_name + (*ite));
}
}
}
bool CMaDirOperation::chang_file_type(const string &file_name, const std::string &s_file_type, const std::string &d_file_type, std::string &changed_file_name) const
{
if(!is_file_type(file_name, s_file_type))
{
cout<<"File name "<<file_name.c_str()<<" is not mached file type "<<s_file_type.c_str()<<" in CMaDirOperation::chang_file_type()."<<endl;
return false;
}
changed_file_name = file_name;
int len = s_file_type.length();
changed_file_name.replace(changed_file_name.length()-len, len, d_file_type);
return true;
}
bool CMaDirOperation::is_file_type(const string &file_name, const string &file_type) const
{
string str = file_name;
int len = file_type.length();
if(0 == str.substr(str.length()-len, len).compare(file_type))
return true;
return false;
}以上就是boost中如何使用filesystem庫,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降摹OM隳芡ㄟ^這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
當(dāng)前名稱:boost中如何使用filesystem庫
文章鏈接:http://chinadenli.net/article8/gdggop.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計、網(wǎng)站排名、響應(yīng)式網(wǎng)站、Google、App設(shè)計、網(wǎng)站收錄
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)