fix ReadString so fixed length strings don't have garbage at the end up to the max length specified

This commit is contained in:
Gered 2012-12-12 15:16:33 -05:00
parent e3d515f34a
commit 646cc690cc

View file

@ -1,4 +1,9 @@
#include "files.h" #include "files.h"
#include <stdio.h>
#if defined(_MSC_VER) && defined(_WIN32) && !defined(snprintf)
#define snprintf _snprintf
#endif
void ReadString(FILE *fp, std::string &buffer, int fixedLength) 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) if (fixedLength > 0)
{ {
char *temp = new char[fixedLength];
fread(temp, 1, fixedLength, fp);
for (int i = 0; i < fixedLength; ++i) for (int i = 0; i < fixedLength; ++i)
{ {
fread(&c, 1, 1, fp); c = temp[i];
if (c != '\0') if (c != '\0')
buffer += c; buffer += c;
else
break;
} }
} }
else else