string_file.hpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // filesystem/string_file.hpp --------------------------------------------------------//
  2. // Copyright Beman Dawes 2015
  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. #ifndef BOOST_FILESYSTEM_STRING_FILE_HPP
  7. #define BOOST_FILESYSTEM_STRING_FILE_HPP
  8. #include <string>
  9. #include <boost/filesystem/fstream.hpp>
  10. #include <boost/filesystem/operations.hpp>
  11. namespace boost
  12. {
  13. namespace filesystem
  14. {
  15. inline
  16. void save_string_file(const path& p, const std::string& str)
  17. {
  18. filesystem::ofstream file;
  19. file.exceptions(std::ofstream::failbit | std::ofstream::badbit);
  20. file.open(p, std::ios_base::binary);
  21. file.write(str.c_str(), str.size());
  22. }
  23. inline
  24. void load_string_file(const path& p, std::string& str)
  25. {
  26. filesystem::ifstream file;
  27. file.exceptions(std::ifstream::failbit | std::ifstream::badbit);
  28. file.open(p, std::ios_base::binary);
  29. std::size_t sz = static_cast<std::size_t>(filesystem::file_size(p));
  30. str.resize(sz, '\0');
  31. file.read(&str[0], sz);
  32. }
  33. } // namespace filesystem
  34. } // namespace boost
  35. #endif // include guard