From 646cc690cc54afd371539aca0297dd0545da5216 Mon Sep 17 00:00:00 2001 From: gered Date: Wed, 12 Dec 2012 15:16:33 -0500 Subject: [PATCH] fix ReadString so fixed length strings don't have garbage at the end up to the max length specified --- src/util/files.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/util/files.cpp b/src/util/files.cpp index 38253dc..4cf87fc 100644 --- a/src/util/files.cpp +++ b/src/util/files.cpp @@ -1,4 +1,9 @@ #include "files.h" +#include + +#if defined(_MSC_VER) && defined(_WIN32) && !defined(snprintf) +#define snprintf _snprintf +#endif void ReadString(FILE *fp, std::string &buffer, int fixedLength) { @@ -6,11 +11,17 @@ void ReadString(FILE *fp, std::string &buffer, int fixedLength) if (fixedLength > 0) { + char *temp = new char[fixedLength]; + fread(temp, 1, fixedLength, fp); + for (int i = 0; i < fixedLength; ++i) { - fread(&c, 1, 1, fp); + c = temp[i]; + if (c != '\0') buffer += c; + else + break; } } else