c++ 利用boost 實現檔案操作
阿新 • • 發佈:2019-01-03
對資料夾裡面的檔案進行遍歷操作是基本技能之一,python,perl以及bash等指令碼都很好的實現了檔案遍歷方法,對於c/c++來說,只能通過系統自定的api獲取。雖然資料夾操作本身是呼叫作業系統核心的介面,但畢竟介面不夠友好。
boost不愧是準標準庫,filesystem提供了極為簡便的方法,如下所示:
[cpp] view plain copy
// filesystem tut3.cpp ---------------------------------------------------------------//
// Copyright Beman Dawes 2009
// Distributed under the Boost Software License, Version 1.0.
// See http://www.boost.org/LICENSE_1_0.txt
// Library home page: http://www.boost.org/libs/filesystem
#include <iostream>
#include <iterator>
#include <algorithm>
#include <boost/filesystem.hpp>
using namespace std;
using namespace boost::filesystem;
int main(int argc, char * argv[])
{
if (argc < 2)
{
cout << "Usage: tut3 path\n";
return 1;
}
path p (argv[1]); // p reads clearer than argv[1] in the following code
try
{
if (exists(p)) // does p actually exist?
{
if (is_regular_file(p)) // is p a regular file?
cout << p << " size is " << file_size(p) << '\n';
else if (is_directory(p)) // is p a directory?
{
cout << p << " is a directory containing:\n";
copy(directory_iterator(p), directory_iterator(), // directory_iterator::value_type
ostream_iterator<directory_entry>(cout, "\n")); // is directory_entry, which is
// converted to a path by the
// path stream inserter
}
else
cout << p << " exists, but is neither a regular file nor a directory\n";
}
else
cout << p << " does not exist\n";
}
catch (const filesystem_error& ex)
{
cout << ex.what() << '\n';
}
return 0;
}
然而,每次寫程式都要附上如此一大篇程式碼,很不美觀。故作了簡單封裝。
.h檔案
[cpp] view plain copy
#ifndef DIRFILEOPT_HHHH
#define DIRFILEOPT_HHHH
#include <iostream>
#include <vector>
#include <string>
using std::vector;
using std::string;
class CFileOpt
{
private:
bool m_bIsDir;
bool m_bIsFile;
char* m_pFileName;
bool mDirOrFile();
public:
CFileOpt(char*);
vector<string>& mGetSubFiles(vector<string>& lstpFileNames);
~CFileOpt();
};#endif
.cpp檔案
[cpp] view plain copy
#define _SCL_SECURE_NO_WARNINGS
#include "FileOpt.h"
#include <iterator>
#include <algorithm>
#include <boost/filesystem.hpp>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string.hpp>
using namespace std;
using namespace boost::filesystem;
bool CFileOpt::mDirOrFile()
{
if(NULL == m_pFileName)
return false;
path p(m_pFileName);
try{
if(exists(p)){
if (is_regular_file(p))
m_bIsFile = true;
else if (is_directory(p)){
m_bIsDir = true;
}
}else{
return false;
}
}catch (const filesystem_error& ex){
#ifdef DEBUG
printf(ex.what());
#endif
}
return true;
}
CFileOpt::CFileOpt(char* pfilename):
m_pFileName(pfilename),m_bIsDir(false),m_bIsFile(false){
mDirOrFile();
}
vector<string>& CFileOpt::mGetSubFiles(vector<string>& lstpFileNames)
{
if(m_bIsDir){
path p(m_pFileName);
typedef vector<path> vec; // store paths,
vec pathes;
#ifdef DEBUG
copy(directory_iterator(p), directory_iterator(),ostream_iterator<directory_entry>(cout,"\n"));
#endif
copy(directory_iterator(p), directory_iterator(), back_inserter(pathes));
for(auto iter = pathes.begin();iter != pathes.end();iter ++){
lstpFileNames.push_back(iter->generic_string());
}
return lstpFileNames;
}else{
#ifdef DEBUG
printf("No SubFiles In %s\n",m_pFileName);
#endif
}
return lstpFileNames;
}
CFileOpt::~CFileOpt(){
m_pFileName = NULL;
}
呼叫建構函式,傳入資料夾的名字,通過mGetSubFiles()函式就可以返回資料夾內的檔案路徑