From eaa8efb950be5d8f1803a99b06f76cf398c67cb8 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Thu, 16 Jun 2022 14:56:45 -0700 Subject: [PATCH] Fix ofstream handling in msvc --- include/fmt/ostream.h | 4 +++- test/ostream-test.cc | 7 +++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/include/fmt/ostream.h b/include/fmt/ostream.h index 9b870820..f58453dd 100644 --- a/include/fmt/ostream.h +++ b/include/fmt/ostream.h @@ -82,7 +82,9 @@ template class filebuf_access; inline bool write(std::filebuf& buf, fmt::string_view data) { - print(get_file(buf), data); + FILE* f = get_file(buf); + if (!f) return false; + print(f, data); return true; } inline bool write(std::wfilebuf&, fmt::basic_string_view) { diff --git a/test/ostream-test.cc b/test/ostream-test.cc index b6d0085d..6a95b9ce 100644 --- a/test/ostream-test.cc +++ b/test/ostream-test.cc @@ -5,6 +5,8 @@ // // For the license information refer to format.h. +#include + #include "fmt/format.h" using fmt::runtime; @@ -297,3 +299,8 @@ TEST(ostream_test, is_formattable) { EXPECT_TRUE(fmt::is_formattable()); EXPECT_TRUE(fmt::is_formattable>()); } + +TEST(ostream_test, closed_ofstream) { + std::ofstream ofs; + fmt::print(ofs, "discard"); +}