Wrapping FILE* with custom std::ostream

As Ben Voigt points out, you want to subclass streambuf. There are pages on the University of Southern California’s website which have the documentation, header, and source for a GNU implementation of a streambuf subclass (stdiobuf) that wraps a FILE*. It has some dependencies on the library it is a part of (GroovX), but those should be easily to remove (I would begin by removing all references to GVX_TRACE).

Interestingly, it also provides a minimalistic subclass (stdiostream) of std::iostream, in spite of what Ben Voigt said. But this does not seem to be necessary, as the rdbuf (“read buffer”/set the stream buffer) method which the stdiostream class uses to connect the stdiobuf class to a stream object is publicly accessible.

You can find more about subclassing streambuf here (look particularly at the bottom of the page, which discussing the virtual functions). The implementation linked above overrides sync, underflow (to support input) and overflow (to support output).

Further notes about the linked implementation:

  • The init method uses the setg and setp methods to set the pointers for the input and output sequences.
  • The line const int num = pptr()-pbase(); is calculating the number of characters to flush by subtracting the base output pointer from the current output pointer (“put pointer”).
  • The variable unhelpfully named om is the mode parameter.
  • The variable named fd is the file descriptor.

Leave a Comment