using boost to list files

I'm trying to understand how to use boost. Specifically the filesystem part on getting a list of file in a certain directory. My end goal is to insert each file as a string into a vector string. I found this code snippet but I cant seem to fix the errors?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include "boost/filesystem.hpp"
#include <map>


int main(){
    
    namespace fs = boost::filesystem;
    fs::path someDir("/home/metulburr/");
    fs::directory_iterator end_iter;

    typedef std::multimap<std::time_t, fs::path> result_set_t;
    result_set_t result_set;

    if ( fs::exists(someDir) && fs::is_directory(someDir)){
      for( fs::directory_iterator dir_iter(someDir) ; dir_iter != end_iter ; ++dir_iter)
      {
        if (fs::is_regular_file(dir_iter->status()) )
        {
          result_set.insert(result_set_t::value_type(fs::last_write_time(dir_iter->status()), *dir_iter);
        }
      }
    }
}


The documentation for boost is the worst i have ever seen. So I havent been able to find much regarding listing files in a directory. Which seems kind of odd as that would seem like a more used feature. I have figured out the way of listing files in linux using dirent.h, but I am now focusing on how to do it in boost.

EDIT:
i forgot the error i get:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
test.cpp: In function ‘int main()’:
test.cpp:20:92: error: no matching function for call to ‘last_write_time(boost::filesystem3::file_status)’
           result_set.insert(result_set_t::value_type(fs::last_write_time(dir_iter->status()), *dir_iter);
                                                                                            ^
test.cpp:20:92: note: candidates are:
In file included from /usr/include/boost/filesystem.hpp:36:0,
                 from test.cpp:2:
/usr/include/boost/filesystem/v3/operations.hpp:479:8: note: void boost::filesystem3::last_write_time(const boost::filesystem3::path&, time_t, boost::system::error_code&)
   void last_write_time(const path& p, const std::time_t new_time, system::error_code& ec)
        ^
/usr/include/boost/filesystem/v3/operations.hpp:479:8: note:   candidate expects 3 arguments, 1 provided
/usr/include/boost/filesystem/v3/operations.hpp:476:8: note: void boost::filesystem3::last_write_time(const boost::filesystem3::path&, time_t)
   void last_write_time(const path& p, const std::time_t new_time)
        ^
/usr/include/boost/filesystem/v3/operations.hpp:476:8: note:   candidate expects 2 arguments, 1 provided
/usr/include/boost/filesystem/v3/operations.hpp:473:15: note: time_t boost::filesystem3::last_write_time(const boost::filesystem3::path&, boost::system::error_code&)
   std::time_t last_write_time(const path& p, system::error_code& ec)
               ^
/usr/include/boost/filesystem/v3/operations.hpp:473:15: note:   candidate expects 2 arguments, 1 provided
/usr/include/boost/filesystem/v3/operations.hpp:470:15: note: time_t boost::filesystem3::last_write_time(const boost::filesystem3::path&)
   std::time_t last_write_time(const path& p) {return detail::last_write_time(p);}
               ^
/usr/include/boost/filesystem/v3/operations.hpp:470:15: note:   no known conversion for argument 1 from ‘boost::filesystem3::file_status’ to ‘const boost::filesystem3::path&’

Last edited on
1. last_write_time takes a path, not a status

http://www.boost.org/doc/libs/1_46_1/libs/filesystem/v3/doc/reference.html#last_write_time

std::time_t last_write_time(const path& p);
std::time_t last_write_time(const path& p, system::error_code& ec);

Returns: The time of last data modification of p, determined as if by the value of the POSIX stat structure member st_mtime obtained as if by POSIX stat().

2. your map takes a time_t and a path, not a time_t and a directory_entry (which is what deref-ing the interator returns)

1
2
3
4
5
        fs::directory_entry& entry = (*dir_iter);
        if (fs::is_regular_file(entry.status()) )
        {
          result_set.insert(result_set_t::value_type(fs::last_write_time(entry.path()), entry.path()));
        }


Andy

PS exists() is unnecessary when using is_directory() -- it's got to exist if it's a directory.
Last edited on
Topic archived. No new replies allowed.