title: Using sprintf with std::string in C++
body: <p>I am using <code>sprintf</code> function in C++ 11, in the following way:</p>
  
  <pre><code>std::string toString()
  {
      std::string output;
      uint32_t strSize=512;
      do
      {
          output.reserve(strSize);
          int ret = sprintf(output.c_str(), "Type=%u Version=%u ContentType=%u contentFormatVersion=%u magic=%04x Seg=%u",
              INDEX_RECORD_TYPE_SERIALIZATION_HEADER,
              FORAMT_VERSION,
              contentType,
              contentFormatVersion,
              magic,
              segmentId);
  
          strSize *= 2;
      } while (ret &lt; 0);
  
      return output;
  }
  </code></pre>
  
  <p>Is there a better way to do this, than to check every time if the reserved space was enough? For future possibility of adding more things.</p>