Search code examples
c++fmt

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

  • 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.