• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //  filesystem tut4.cpp  ---------------------------------------------------------------//
2 
3 //  Copyright Beman Dawes 2009
4 
5 //  Distributed under the Boost Software License, Version 1.0.
6 //  See http://www.boost.org/LICENSE_1_0.txt
7 
8 //  Library home page: http://www.boost.org/libs/filesystem
9 
10 #include <iostream>
11 #include <vector>
12 #include <algorithm>
13 #include <boost/filesystem.hpp>
14 using std::cout;
15 using namespace boost::filesystem;
16 
main(int argc,char * argv[])17 int main(int argc, char* argv[])
18 {
19   if (argc < 2)
20   {
21     cout << "Usage: tut4 path\n";
22     return 1;
23   }
24 
25   path p (argv[1]);
26 
27   try
28   {
29     if (exists(p))
30     {
31       if (is_regular_file(p))
32         cout << p << " size is " << file_size(p) << '\n';
33 
34       else if (is_directory(p))
35       {
36         cout << p << " is a directory containing:\n";
37 
38         std::vector<path> v;
39 
40         for (auto&& x : directory_iterator(p))
41           v.push_back(x.path());
42 
43         std::sort(v.begin(), v.end());
44 
45         for (auto&& x : v)
46           cout << "    " << x.filename() << '\n';
47       }
48       else
49         cout << p << " exists, but is not a regular file or directory\n";
50     }
51     else
52       cout << p << " does not exist\n";
53   }
54 
55   catch (const filesystem_error& ex)
56   {
57     cout << ex.what() << '\n';
58   }
59 
60   return 0;
61 }
62