fork(1) download
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5. class MyFileBuf : public std::filebuf
  6. {
  7. protected:
  8. virtual int_type sync()
  9. {
  10. // breakpoint here never fires
  11. std::cout << "sync" << std::endl;
  12. return std::filebuf::sync();
  13. };
  14.  
  15. virtual std::streamsize xsputn( const char_type* p, std::streamsize n )
  16. {
  17. // breakpoint here never fires
  18. std::cout << "xsputn" << std::endl;
  19. return std::filebuf::xsputn( p, n );
  20. };
  21.  
  22. virtual int_type overflow( int_type c = traits_type::eof() )
  23. {
  24. // breakpoint here never fires
  25. std::cout << "overflow" << std::endl;
  26. return std::filebuf::overflow( c );
  27. };
  28. };
  29.  
  30. class MyFileStream : public std::ostream
  31. {
  32. public:
  33. MyFileStream() : std::ostream( 0 ) { init( &buf_ ); };
  34. MyFileStream( const char* filename, std::ios_base::openmode mode = std::ios_base::out )
  35. : std::ostream( 0 )
  36. {
  37. init( &buf_ );
  38. this->open( filename, mode );
  39. }
  40.  
  41. bool is_open() const { return buf_.is_open(); };
  42.  
  43. void close() { buf_.close(); };
  44.  
  45. void open( const char* filename, std::ios_base::openmode mode = std::ios_base::out )
  46. {
  47. buf_.open( filename, mode );
  48. };
  49.  
  50. std::filebuf* rdbuf() { return &buf_; };
  51.  
  52. private:
  53. MyFileBuf buf_;
  54. };
  55.  
  56. int main()
  57. {
  58. MyFileStream fs( "test.txt" );
  59. fs << "this is a test" << std::endl;
  60. return 0;
  61. }
  62.  
Success #stdin #stdout 0.01s 2864KB
stdin
Standard input is empty
stdout
xsputn
overflow
sync