jeudi 28 avril 2016

using sprintf with std::string in c++

I am using sprintf function in C++ 11, in the following way:

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 < 0);
        return output;

    }   

Is there any better way to do this, than to check every time if the reserved space was enough? For future possibility of adding more things.

Aucun commentaire:

Enregistrer un commentaire