Best way to safely printf to a string?

You can use:

std::snprintf if you are working with a char*

std::stringstream if you want to use strings (not same as printf but will allow you to easily manipulate the string using the normal stream functions).

boost::format if you want a function similar to printf that will work with streams. (as per jalf in comments)

fmt::format which is has been standardized since c++20 std::format


The snprintf() function prints to a string, but only as much as the length given to it.

Might be what you're looking for...

Tags:

C++

Stl