diff --git a/elfio/elfio.hpp b/elfio/elfio.hpp
index 428d76149e93405dd8ee694c6bd537904ebae0bc..d798961dbadfed114dec7ab477f08ab8b792c58f 100644
--- a/elfio/elfio.hpp
+++ b/elfio/elfio.hpp
@@ -148,12 +148,11 @@ class elfio
     {
         std::ofstream f( file_name.c_str(), std::ios::out | std::ios::binary );
 
-        if ( !f ) {
+        if ( !f || !header) {
             return false;
         }
 
         bool is_still_good = true;
-
         // Define layout specific header fields
         // The position of the segment table is fixed after the header.
         // The position of the section table is variable and needs to be fixed