What's the canonical way to use the fmtlib with wofstream

928 views Asked by At

We're switching to the fmt library and aim to rewrite many wide character output stream based formattings.

I found fmt::output_file which returns a class ostream : private detail::buffer<char> instance but it looks like there's no support for wchar_t.

What's the canonical way to use the format library in wide character output stream scenarios?

The only way i can think of is to manually open a std::wofstream and use fmt::format. Is this on a roadmap or am i missing something?

1

There are 1 answers

1
vitaut On BEST ANSWER

fmt/ostream.h provides an fmt::print overload that can write to wofstream:

#include <fmt/ostream.h>
#include <fstream>

int main() {
  std::wofstream ofs("foo");
  fmt::print(ofs, L"The answer is {}.\n", 42);
}

godbolt

That said, I would recommend avoiding wchar_t if you care about portability and performance.