file_contents_as_string.cpp 597 B

12345678910111213141516171819202122232425
  1. #include "file_contents_as_string.h"
  2. #include <fstream>
  3. #include <cstdio>
  4. IGL_INLINE bool igl::file_contents_as_string(
  5. const std::string file_name,
  6. std::string & content)
  7. {
  8. std::ifstream ifs(file_name.c_str());
  9. // Check that opening the stream worked successfully
  10. if(!ifs.good())
  11. {
  12. fprintf(
  13. stderr,
  14. "IOError: file_contents_as_string() cannot open %s\n",
  15. file_name.c_str());
  16. return false;
  17. }
  18. // Stream file contents into string
  19. content = std::string(
  20. (std::istreambuf_iterator<char>(ifs)),
  21. (std::istreambuf_iterator<char>()));
  22. return true;
  23. }