fork(5) download
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. class my_ostream
  5. {
  6. public:
  7. my_ostream() : my_fstream("some_file.txt") {}; // check if opening file succeeded!!
  8. // for regular output of variables and stuff
  9. template<typename T> my_ostream& operator<<(const T& something)
  10. {
  11. std::cout << something;
  12. my_fstream << something;
  13. return *this;
  14. }
  15. // for manipulators like std::endl
  16. typedef std::ostream& (*stream_function)(std::ostream&);
  17. my_ostream& operator<<(stream_function func)
  18. {
  19. func(std::cout);
  20. func(my_fstream);
  21. return *this;
  22. }
  23. private:
  24. std::ofstream my_fstream;
  25. };
  26.  
  27. my_ostream mout;
  28.  
  29. int main()
  30. {
  31. mout << "Hello Worlds!" << std::endl;
  32. }
Success #stdin #stdout 0s 3064KB
stdin
Standard input is empty
stdout
Hello Worlds!