title: Redirect file descriptor into memory
body: <p>I am working with a file API that only provides a <code>const char* filename</code> interface (accepting <code>-</code> for <code>stdout</code>) when writing files. I would instead like the output to be written into memory, so I can then pass the data elsewhere.</p>
  
  <p>I can use <code>dup2</code> to redirect <code>stdout</code> to an arbitrary file descriptor and something like <code>fmemopen</code>/<code>open_memstream</code> as my sink. However, the memory stream functions expect a size, which -- in my case -- I don't know in advance and could be arbitrarily large.</p>
  
  <p>The data I need to access, however, does have a fixed length and offset within what's being produced (e.g., out of 1MB, I need the 64KB starting at 384KB, etc.). As such, is there a way to set up a circular buffer with <code>fmemopen</code>/<code>open_memstream</code> that just keeps being rewritten until reaching the offset in question? (I realise this is inefficient, but there's no ability to seek.)</p>
  
  <p>Or is this the wrong approach? I've read a bit about memory mapped files and that seems to be similar to what I'm trying to achieve, but it's not something I know much about...</p>
  
  <hr>
  
  <p><em>EDIT</em> Just to be clear, I cannot write anything to disk.</p>