What is the C++ idiom to replace snprintf(3)?
<p>I have some C++ code which needs to generate an error message when parsing a certain file header fails. In this case, I need to ensure that a certain 4 byte field in the header is "OggS", and if it is not, return an error message like "invalid capture_pattern: 'FooB'; expecting 'OggS'". My code looks something like this:</p>

<pre><code>const string OggPage::parseHeader(void) {
  read(fd, capture_pattern, sizeof(capture_pattern)); // error handling omitted
  if (strncmp(capture_pattern, CAPTURE_PATTERN, sizeof(capture_pattern)) != 0) {
    char err[256];
    snprintf(err, sizeof(err), "Failed to read %d bytes from file descriptor %d: %s\n", sizeof(capture_pattern), fd, err);
    return err;
  }

  return "Everything was A-OK!";
}
</code></pre>

<p>What is the standard C++ idiom for building a string from other datatypes? I'm not wedded to the <code>printf(3)</code>-style format here, so feel free to suggest anything that works. :)</p>

<p>Just as a note, please don't suggest <code>&lt;iostream&gt;</code> replacements for the actual reading--there is a reason that I want to use the C standard library for I/O here. :)</p>
