Aman said:
Yes, I have this problem. But I think that 'vsnprintf' of standard C++
with a simple wrap can provides the same function, right ?
The function vsnprintf() is not in the current standard. But it is in the
draft for the next revision.
This is the wrapper that I added to my library:
bool strprintf ( std::string & buffer, char const * format, ... ) {
while ( true ) {
buffer.resize( buffer.capacity() );
int old_length = buffer.size();
std::va_list aq;
va_start( aq, format );
int length_needed =
vsnprintf( &buffer[0], old_length + 1, format, aq );
va_end( aq );
if ( length_needed < 0 ) {
return ( false );
}
buffer.resize( length_needed );
if ( length_needed <= old_length ) {
return ( true );
}
}
}
Note that this has formally undefined behavior since it assumes (a) that
std::string is contiguous (this is in the current working draft) and (b)
that one can safely write a '\0' at buffer[ buffer.size() ] (works for the
STL implementation I am using).
Best
Kai-Uwe Bux