'using libfmt to format to a string

using libfmt to print to a file is very convenient:

auto file = fmt::output_file(filename);
file.print(...);

But how can I format to a memory buffer, ultimatiley converting to a string? I would imagine something like

auto buf = some_buffer_object{};
buf.print(...);
std::string s = buf.get_string();

But I can find no such buffer type in the documentation (fmt::memory_buffer seems related, but does not work like this).

Important: I need multiple calls to print, so auto s = fmt::format(...) is not an option.



Solution 1:[1]

how can I format to a memory buffer, ultimatiley converting to a string?

Use format_to and print to an iterator the appends to a string.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 KamilCuk