对文件夹里面的文件进行遍历操作是基本技能之一,python,perl以及bash等脚本都很好的实现了文件遍历方法,对于c/c++来说,只能通过系统自定的api获取。虽然文件夹操作本身是调用操作系统内核的接口,但毕竟接口不够友好。boost不愧是准标准库,filesystem提供了极为简便的方法,如下所示:
然而,每次写程序都要附上如此一大篇代码,很不美观。故作了简单封装。
- // 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文件
- #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
调用构造函数,传入文件夹的名字,通过mGetSubFiles()函数就可以返回文件夹内的文件路径。
- #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;
- }
c++ 利用boost 实现文件操作
最新推荐文章于 2023-10-14 16:48:59 发布