diff --git a/ChangeLog b/ChangeLog
index f50f7c8f..a2e42412 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2011-08-12  Joerg Wunsch <j.gnu@uriah.heep.sax.de>
+
+	Contributed by tixiv@gmx.net:
+	bug #33345: File auto detection as binary doesn't open
+	file in binary mode on Windows
+	* fileio.c: Move the decision about opening files in
+	binary mode until before the fopen() call.
+
 2011-06-16  Thomas Fischl <tfischl@gmx.de>
 
 	* avrdude.conf.in: Fix part id of ATtiny9.
diff --git a/fileio.c b/fileio.c
index f4ff016b..5af91bab 100644
--- a/fileio.c
+++ b/fileio.c
@@ -1034,21 +1034,6 @@ int fileio(int op, char * filename, FILEFMT format,
   if (rc < 0)
     return -1;
 
-#if defined(WIN32NATIVE)
-  /* Open Raw Binary format in binary mode on Windows.*/
-  if(format == FMT_RBIN)
-  {
-      if(fio.op == FIO_READ)
-      {
-          fio.mode = "rb";
-      }
-      if(fio.op == FIO_WRITE)
-      {
-          fio.mode = "wb";
-      }
-  }
-#endif
-
   /* point at the requested memory buffer */
   buf = mem->buf;
   if (fio.op == FIO_READ)
@@ -1100,6 +1085,21 @@ int fileio(int op, char * filename, FILEFMT format,
     }
   }
 
+#if defined(WIN32NATIVE)
+  /* Open Raw Binary format in binary mode on Windows.*/
+  if(format == FMT_RBIN)
+  {
+      if(fio.op == FIO_READ)
+      {
+          fio.mode = "rb";
+      }
+      if(fio.op == FIO_WRITE)
+      {
+          fio.mode = "wb";
+      }
+  }
+#endif
+
   if (format != FMT_IMM) {
     if (!using_stdio) {
       f = fopen(fname, fio.mode);