Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Zortech C++ Language Reference - streams: class filebuf http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
   Streams: class filebuf
   The  filebuf  class  is also defined in stream.hpp.  It  has  its  own
   constructors,  and  redefines  the  virtual  functions  overflow   and
   underflow, since in this case it is possible to do something useful in
   each of these cases: either fill the buffer from the file or empty  it
   to a file.

   class filebuf : public streambuf
   {
   int fd;
   int opened;
   public:

   // Functions for buffer full and empty respectively
   int overflow(int c);
   int underflow();

   // File manipulations
   filebuf* open(char *,open_mode);
   int close();

   // Constructors
   filebuf()
   { fp = 0; opened = 0; }
   filebuf(int d)
   { fd = d; fp = 0; opened = 1; }
   filebuf(FILE* p)
   { fp = p; opened = 1; }
   filebuf(int d, char *buf, int buflen)
   : (buf,buflen)
   { fd = d; fp = 0; opened = 1; }

   // Destructor
   ~filebuf() { close(); }
   };

   Like  many  derived classes, the implementation of  class  filebuf  is
   quite  simple. We have already discussed underflow and  overflow.  The
   function filebuf::open opens as existing file or creates a new one  in
   a  required mode. It returns NULL if it fails, otherwise a pointer  to
   itself. The complimentary function filebuf::close closes the file.  It
   returns  zero if successful, or -1 if there is an error. Note that  if
   the  filebuf is a layer over stdio, close will return zero. It is  not
   regarded  as  part  of the function of a  filestream  to  close  stdio
   streams.

   There are four constructors. The first takes no arguments and  creates
   a  null  filebuf. Of course, filebuf::open will need to be  called  to
   initialize it.

   The second constructor takes an integer file descriptor as argument. A
   filebuf  is created dynamically, and when that is  subsequently  used,
   its  streambuf  part  will attempt to allocate  a  1024  byte  buffer.
   Subsequent calls to open will be silently ignored.

   Constructor three is the one used to associate the stdio streams  with
   ostreams. The sequence is as follows:

   filebuf cout_file(stdout);
   ostream cout(&cout_file);

   filebuf cerr_file(stderr);
   ostream cerr(&cerr_file);

   filebuf cprn_file(stdprn);
   ostream cprn(&cprn_file);

   filebuf caux_file(stdaux);
   ostream caux(&caux_file);

   A final constructor allows file descriptor, buffer, and buffer  length
   to be specified.

   The  implementation  of  the  filebuf  destructor  just  flushes   any
   remaining  contents  of the buffer to the file, then  closes  it.  The
   declaration of the destructor in streambuf as virtual means that it is
   possible to use a sequence like:

   streambuf *sbp = getbuffer();
   // getbuffer might decide to
   // return a streambuf or a
   // filebuf.

   ostream os(sbp);
   delete sbp;
   The deletion of the buffer would cause appropriate steps to be  taken,
   such as closing the file. regardless of what type it turned out to be.


Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson