利用boost库遍历文件夹

最近用到boost库,其中遍历文件夹还很方便。mark下。

#include 
#include 

using namespace boost;
namespace fs = boost::filesystem2;

int main()
{
    string dir = "path";

    fs::path template_dir( dir, fs::native );

    //no such directory
    if( !fs::exists( template_dir ) || !fs::is_directory( template_dir ))
    {
        assert( 0 && "path is incorrect" );
        return -1;
    }
    //search the files
    fs::directory_iterator start_iter( template_dir ), end_iter;

    for( ; start_iter != end_iter; ++start_iter )
    {
        if( !fs::is_directory( *start_iter ) )
        {
            string file_path = start_iter->path().file_string();
            string file_name = start_iter->filename();
            if( start_iter->path().extension() != "xml" )
            {
                continue;
            }
	    //Now we have the file_path, file_name, then we can read the file and ...

        }
     }
    return 0;
}


你可能感兴趣的:(Boost库)