stdin_to_temp.cpp 679 B

12345678910111213141516171819202122232425262728293031
  1. #include "stdin_to_temp.h"
  2. #include <iostream>
  3. IGL_INLINE bool igl::stdin_to_temp(FILE ** temp_file)
  4. {
  5. // get a temporary file
  6. *temp_file = tmpfile();
  7. if(*temp_file == NULL)
  8. {
  9. fprintf(stderr,"IOError: temp file could not be created.\n");
  10. return false;
  11. }
  12. char c;
  13. // c++'s cin handles the stdind input in a reasonable way
  14. while (std::cin.good())
  15. {
  16. c = std::cin.get();
  17. if(std::cin.good())
  18. {
  19. if(1 != fwrite(&c,sizeof(char),1,*temp_file))
  20. {
  21. fprintf(stderr,"IOError: error writing to tempfile.\n");
  22. return false;
  23. }
  24. }
  25. }
  26. // rewind file getting it ready to read from
  27. rewind(*temp_file);
  28. return true;
  29. }