file_contents_as_string.cpp 943 B

1234567891011121314151617181920212223242526272829303132
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <alecjacobson@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "file_contents_as_string.h"
  9. #include <fstream>
  10. #include <cstdio>
  11. IGL_INLINE bool igl::file_contents_as_string(
  12. const std::string file_name,
  13. std::string & content)
  14. {
  15. std::ifstream ifs(file_name.c_str());
  16. // Check that opening the stream worked successfully
  17. if(!ifs.good())
  18. {
  19. fprintf(
  20. stderr,
  21. "IOError: file_contents_as_string() cannot open %s\n",
  22. file_name.c_str());
  23. return false;
  24. }
  25. // Stream file contents into string
  26. content = std::string(
  27. (std::istreambuf_iterator<char>(ifs)),
  28. (std::istreambuf_iterator<char>()));
  29. return true;
  30. }