• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //  filesystem tut3.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 <boost/filesystem.hpp>
12 using std::cout;
13 using namespace boost::filesystem;
14 
main(int argc,char * argv[])15 int main(int argc, char* argv[])
16 {
17   if (argc < 2)
18   {
19     cout << "Usage: tut3 path\n";
20     return 1;
21   }
22 
23   path p (argv[1]);
24 
25   try
26   {
27     if (exists(p))
28     {
29       if (is_regular_file(p))
30         cout << p << " size is " << file_size(p) << '\n';
31 
32       else if (is_directory(p))
33       {
34         cout << p << " is a directory containing:\n";
35 
36         for (const directory_entry& x : directory_iterator(p))
37           cout << "    " << x.path() << '\n';
38       }
39       else
40         cout << p << " exists, but is not a regular file or directory\n";
41     }
42     else
43       cout << p << " does not exist\n";
44   }
45 
46   catch (const filesystem_error& ex)
47   {
48     cout << ex.what() << '\n';
49   }
50 
51   return 0;
52 }
53