tut3.cpp 1.1 KB

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