From 9ae9c1b2a65224daa0ae74401ce69888881ef631 Mon Sep 17 00:00:00 2001 From: csilvers Date: Wed, 27 Jul 2011 01:01:06 +0000 Subject: [PATCH] Make sure we open files for reading or writing in binary mode. That way, \r isn't silently stripped on windows. R=jad --- src/base/fileutil.h | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/base/fileutil.h b/src/base/fileutil.h index 16adde5..39eab71 100644 --- a/src/base/fileutil.h +++ b/src/base/fileutil.h @@ -72,7 +72,17 @@ class File { } static File* Open(const char* filename, const char* mode) { - FILE* fp = fopen(filename, mode); + char binary_mode[3]; + const char* mode_to_use = mode; + if ((mode[0] == 'r' || mode[0] == 'w') && mode[1] == '\0') { + // We add a 'b' to make sure we do the right thing even on + // Windows. On unix, this will be a noop. + binary_mode[0] = mode[0]; + binary_mode[1] = 'b'; + binary_mode[2] = '\0'; + mode_to_use = binary_mode; + } + FILE* fp = fopen(filename, mode_to_use); if (!fp) return NULL; return new File(fp); }