C++ Method: Format String with Arguments

This simple snippet allows formatting strings with arguments like the std::*printf methods, but returns the result as std::string.

#include <stdarg.h>
#include <string>

#define CHAR_BUFFER_SIZE 8192

std::string FormatString(const char* c_str, ...) {
    char buffer[CHAR_BUFFER_SIZE] = {}; 
    va_list arglist;
    va_start(arglist, c_str);
    vsnprintf(buffer, sizeof(buffer), c_str, arglist);
    va_end(arglist);
    return std::string(buffer);
}
Nach oben scrollen