Skip to content

Commit

Permalink
Fix ofstream handling in msvc
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Jun 16, 2022
1 parent fb991e9 commit eaa8efb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
4 changes: 3 additions & 1 deletion include/fmt/ostream.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ template class filebuf_access<filebuf_access_tag,
&filebuf_type::_Myfile>;

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<wchar_t>) {
Expand Down
7 changes: 7 additions & 0 deletions test/ostream-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
//
// For the license information refer to format.h.

#include <fstream>

#include "fmt/format.h"

using fmt::runtime;
Expand Down Expand Up @@ -297,3 +299,8 @@ TEST(ostream_test, is_formattable) {
EXPECT_TRUE(fmt::is_formattable<std::string>());
EXPECT_TRUE(fmt::is_formattable<fmt::detail::std_string_view<char>>());
}

TEST(ostream_test, closed_ofstream) {
std::ofstream ofs;
fmt::print(ofs, "discard");
}

0 comments on commit eaa8efb

Please sign in to comment.