From e1e1bced08d195c9190ac0d96d64c10abc226f3d Mon Sep 17 00:00:00 2001 From: Wouter van Oortmerssen Date: Mon, 28 Sep 2015 15:31:03 -0700 Subject: [PATCH] Fixed LoadFile on Windows --- include/flatbuffers/util.h | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/include/flatbuffers/util.h b/include/flatbuffers/util.h index b9d1f74..67f75d5 100644 --- a/include/flatbuffers/util.h +++ b/include/flatbuffers/util.h @@ -112,10 +112,18 @@ inline bool FileExists(const char *name) { inline bool LoadFile(const char *name, bool binary, std::string *buf) { std::ifstream ifs(name, binary ? std::ifstream::binary : std::ifstream::in); if (!ifs.is_open()) return false; - ifs.seekg(0, std::ios::end); - (*buf).resize(static_cast(ifs.tellg())); - ifs.seekg(0, std::ios::beg); - ifs.read(&(*buf)[0], (*buf).size()); + if (binary) { + // The fastest way to read a file into a string. + ifs.seekg(0, std::ios::end); + (*buf).resize(static_cast(ifs.tellg())); + ifs.seekg(0, std::ios::beg); + ifs.read(&(*buf)[0], (*buf).size()); + } else { + // This is slower, but works correctly on all platforms for text files. + std::ostringstream oss; + oss << ifs.rdbuf(); + *buf = oss.str(); + } return !ifs.bad(); } -- 2.7.4