example.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /**
  2. * Simple example showing how to use stdin_to_temp
  3. *
  4. * Compile with:
  5. * g++ -O3 -o stdin_to_temp_demo stdin_to_temp_demo.cpp
  6. *
  7. * Run examples:
  8. * cat file1 | ./stdin_to_temp_demo
  9. * cat file1 | ./stdin_to_temp_demo | cat >file2
  10. * cat file1 | ./stdin_to_temp_demo dummy1 dummy2 | cat >file2
  11. * ./stdin_to_temp_demo <file1 | cat >file2
  12. * ./stdin_to_temp_demo <file1 >file2
  13. *
  14. */
  15. #include <igl/stdin_to_temp.h>
  16. using namespace igl;
  17. #include <cstdio>
  18. using namespace std;
  19. int main(int argc,char * argv[])
  20. {
  21. // Process arguements and print to stderr
  22. for(int i = 1;i<argc;i++)
  23. {
  24. fprintf(stderr,"argv[%d] = %s\n",i,argv[i]);
  25. }
  26. FILE * temp_file;
  27. bool success = stdin_to_temp(&temp_file);
  28. if(!success)
  29. {
  30. fprintf(stderr,"Fatal Error: could not convert stdin to temp file\n");
  31. // try to close temp file
  32. fclose(temp_file);
  33. return 1;
  34. }
  35. // Do something interesting with the temporary file.
  36. // Read the file and write it to stdout
  37. char c;
  38. // Read file one character at a time and write to stdout
  39. while(fread(&c,sizeof(char),1,temp_file)==1)
  40. {
  41. fwrite(&c,sizeof(char),1,stdout);
  42. }
  43. // close file
  44. fclose(temp_file);
  45. }