tut4.cpp 1.2 KB

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