diff --git a/Geo/GFace.cpp b/Geo/GFace.cpp
index dd32bae429307458ac7efecab083b4bc6265f317..56755edc5f32ea05be4484c201bb0eb596ab8dd6 100644
--- a/Geo/GFace.cpp
+++ b/Geo/GFace.cpp
@@ -1408,7 +1408,7 @@ void GFace::addLayersOfQuads(int nLayers, GVertex *gv, double hmin, double ratio
   SVector3 ez (0, 0, 1);
   std::list<GEdgeLoop>::iterator it = edgeLoops.begin();
   FILE *f = Fopen ("coucou.pos","w");
-  fprintf(f,"View \"\"{\n");
+  if(f) fprintf(f,"View \"\"{\n");
   for (; it != edgeLoops.end(); ++it){
     bool found = false;
     // look if this edge loop has the GVertex as an endpoint
@@ -1454,15 +1454,15 @@ void GFace::addLayersOfQuads(int nLayers, GVertex *gv, double hmin, double ratio
 	  double v = p1.y() + n.y() * hmin;
 	  GPoint gp = point(SPoint2(u,v));
 	  additionalVertices.push_back(new MFaceVertex(gp.x(),gp.y(),gp.z(),this,u,v));
-	  fprintf(f,"SP(%g, %g, 0){1};\n",gp.x(),gp.y());
+	  if(f) fprintf(f,"SP(%g, %g, 0){1};\n",gp.x(),gp.y());
 	}
 	hlayer *= ratio;
 	hmin += hlayer;
       }
-      fprintf(f,"};\n");
+      if(f) fprintf(f,"};\n");
     }
   }
-  fclose(f);
+  if(f) fclose(f);
 }
 
 void GFace::relocateMeshVertices()
diff --git a/Geo/GFaceCompound.cpp b/Geo/GFaceCompound.cpp
index 4df762c62b148f55b5ce6ea55417d9301c279ec3..04076a4503050aba58391c932e56daa00eaeeb13 100644
--- a/Geo/GFaceCompound.cpp
+++ b/Geo/GFaceCompound.cpp
@@ -56,22 +56,6 @@ static void fixEdgeToValue(GEdge *ed, double value, dofManager<double> &myAssemb
   }
 }
 
-/*
-static void printBound(std::vector<MVertex*> &l, int tag)
-{
-  char name[256];
-  sprintf(name, "myBOUND%d.pos", tag);
-  FILE * xyz = Fopen(name,"w");
-  fprintf(xyz,"View \"\"{\n");
-  for(unsigned int i = 0; i < l.size(); i++){
-    MVertex *v = l[i];
-    fprintf(xyz,"SP(%g,%g,%g){%d};\n", v->x(), v->y(), v->z(), i);
-  }
-  fprintf(xyz,"};\n");
-  fclose(xyz);
-}
-*/
-
 static std::vector<MVertex*> getBlob(unsigned int minNbPt, v2t_cont::iterator it,
                                      v2t_cont &adj)
 {
@@ -461,18 +445,20 @@ void GFaceCompound::printFillTris() const
     //std::list<GFace*>::const_iterator itf = _compound.begin();
     sprintf(name, "fillTris-%d.pos", tag());
     FILE * ftri = Fopen(name,"w");
-    fprintf(ftri,"View \"\"{\n");
-    for (std::list<MTriangle*>::iterator it2 = fillTris.begin();
-	 it2 !=fillTris.end(); it2++ ){
-      MTriangle *t = (*it2);
-      fprintf(ftri,"ST(%g,%g,%g,%g,%g,%g,%g,%g,%g){%g,%g,%g};\n",
-	      t->getVertex(0)->x(), t->getVertex(0)->y(), t->getVertex(0)->z(),
-	      t->getVertex(1)->x(), t->getVertex(1)->y(), t->getVertex(1)->z(),
-	      t->getVertex(2)->x(), t->getVertex(2)->y(), t->getVertex(2)->z(),
-	      1., 1., 1.);
-    }
-    fprintf(ftri,"};\n");
-    fclose(ftri);
+    if(ftri){
+      fprintf(ftri,"View \"\"{\n");
+      for (std::list<MTriangle*>::iterator it2 = fillTris.begin();
+           it2 !=fillTris.end(); it2++ ){
+        MTriangle *t = (*it2);
+        fprintf(ftri,"ST(%g,%g,%g,%g,%g,%g,%g,%g,%g){%g,%g,%g};\n",
+                t->getVertex(0)->x(), t->getVertex(0)->y(), t->getVertex(0)->z(),
+                t->getVertex(1)->x(), t->getVertex(1)->y(), t->getVertex(1)->z(),
+                t->getVertex(2)->x(), t->getVertex(2)->y(), t->getVertex(2)->z(),
+                1., 1., 1.);
+      }
+      fprintf(ftri,"};\n");
+      fclose(ftri);
+    }
   }
 }
 
@@ -2885,12 +2871,31 @@ void GFaceCompound::printStuff(int iNewton) const
 
   sprintf(name7, "UVM-%d.pos", (*it)->tag());
 
-  //FILE * uva = Fopen(name0,"w");
   FILE * uvx = Fopen(name1,"w");
+  if(!uvx){
+    Msg::Error("Could not open file '%s'", name1);
+    return;
+  }
   FILE * uvy = Fopen(name2,"w");
+  if(!uvy){
+    Msg::Error("Could not open file '%s'", name2);
+    return;
+  }
   FILE * uvz = Fopen(name3,"w");
+  if(!uvz){
+    Msg::Error("Could not open file '%s'", name3);
+    return;
+  }
   FILE * xyzu = Fopen(name4,"w");
+  if(!xyzu){
+    Msg::Error("Could not open file '%s'", name4);
+    return;
+  }
   FILE * xyzv = Fopen(name5,"w");
+  if(!xyzv){
+    Msg::Error("Could not open file '%s'", name5);
+    return;
+  }
 
   fprintf(uvx, "View \"\"{\n");
   fprintf(uvy, "View \"\"{\n");
diff --git a/Geo/GRbf.cpp b/Geo/GRbf.cpp
index f71323a84174eed390611d3afbbb16d36f0f0211..4c5189990e7a41585cb7c97b58307fec0eed8e7a 100644
--- a/Geo/GRbf.cpp
+++ b/Geo/GRbf.cpp
@@ -61,27 +61,29 @@ static int SphereInEle(void *a, double*c)
 static void printNodes(std::set<MVertex *> myNodes)
 {
   FILE * xyz = Fopen("myNodes.pos","w");
-  fprintf(xyz,"View \"\"{\n");
-  for(std::set<MVertex *>::iterator itv = myNodes.begin(); itv !=myNodes.end(); ++itv){
-    MVertex *v = *itv;
-    fprintf(xyz,"SP(%g,%g,%g){%d};\n", v->x(), v->y(), v->z(), v->getNum());
+  if(xyz){
+    fprintf(xyz,"View \"\"{\n");
+    for(std::set<MVertex *>::iterator itv = myNodes.begin(); itv !=myNodes.end(); ++itv){
+      MVertex *v = *itv;
+      fprintf(xyz,"SP(%g,%g,%g){%d};\n", v->x(), v->y(), v->z(), v->getNum());
+    }
+    fprintf(xyz,"};\n");
+    fclose(xyz);
   }
-  fprintf(xyz,"};\n");
-  fclose(xyz);
 }
 
 static void exportParametrizedMesh(fullMatrix<double> &UV, int nbNodes)
 {
-  FILE *f = Fopen ("UV.pos", "w");
-  fprintf(f,"View  \" uv \" {\n");
-
-  Msg::Info("*** RBF exporting 'UV.pos' ");
-  for(int id = 0; id < nbNodes; id++){
-    fprintf(f,"SP(%g,%g,%g){%d};\n", UV(id,0), UV(id,1), 0.0, id);
+  FILE *f = Fopen("UV.pos", "w");
+  if(f){
+    fprintf(f,"View  \" uv \" {\n");
+    Msg::Info("*** RBF exporting 'UV.pos' ");
+    for(int id = 0; id < nbNodes; id++){
+      fprintf(f,"SP(%g,%g,%g){%d};\n", UV(id,0), UV(id,1), 0.0, id);
+    }
+    fprintf(f,"};\n");
+    fclose(f);
   }
-  fprintf(f,"};\n");
-
-  fclose(f);
 }
 
 GRbf::GRbf(double sizeBox, int variableEps, int rbfFun,
diff --git a/Geo/boundaryLayersData.cpp b/Geo/boundaryLayersData.cpp
index 9d98efc40ab6c62653ba997c465edb3e77a75bb7..deb4be0ba1104f95ae0f316c79105483916d57b6 100644
--- a/Geo/boundaryLayersData.cpp
+++ b/Geo/boundaryLayersData.cpp
@@ -735,25 +735,26 @@ bool buildAdditionalPoints2D(GFace *gf)
       }
     }
   }
-  // DEBUG STUFF
 
+  // DEBUG STUFF
   char name[256];
-  sprintf(name,"points_face_%d.pos",gf->tag());
+  sprintf(name, "points_face_%d.pos", gf->tag());
   FILE *f = Fopen (name,"w");
-  fprintf(f,"View \"\" {\n");
-  for (std::set<MVertex*>::iterator it = _vertices.begin(); it != _vertices.end() ; ++it){
-    MVertex *v = *it;
-    for (int i=0;i<_columns->getNbColumns(v);i++){
-      const BoundaryLayerData &data = _columns->getColumn(v,i);
-      for (unsigned int j = 0; j < data._column.size(); j++){
-	MVertex *blv = data._column[j];
-	fprintf(f,"SP(%g,%g,%g){%d};\n",blv->x(),blv->y(),blv->z(),v->getNum());
+  if(f){
+    fprintf(f,"View \"\" {\n");
+    for (std::set<MVertex*>::iterator it = _vertices.begin(); it != _vertices.end() ; ++it){
+      MVertex *v = *it;
+      for (int i=0;i<_columns->getNbColumns(v);i++){
+        const BoundaryLayerData &data = _columns->getColumn(v,i);
+        for (unsigned int j = 0; j < data._column.size(); j++){
+          MVertex *blv = data._column[j];
+          fprintf(f,"SP(%g,%g,%g){%d};\n",blv->x(),blv->y(),blv->z(),v->getNum());
+        }
       }
     }
+    fprintf(f,"};\n");
+    fclose (f);
   }
-  fprintf(f,"};\n");
-  fclose (f);
-
   // END OF DEBUG STUFF
 
   return 1;
@@ -1328,22 +1329,22 @@ bool buildAdditionalPoints3D(GRegion *gr)
   }
 
   // DEBUG STUFF
-
-  FILE *f = fopen ("POINTS3D.pos","w");
-  fprintf(f,"View \"\" {\n");
-  for (std::set<MVertex*>::iterator it = _vertices.begin(); it != _vertices.end() ; ++it){
-    MVertex *v = *it;
-    for (int i=0;i<_columns->getNbColumns(v);i++){
-      const BoundaryLayerData &data = _columns->getColumn(v,i);
-      for (unsigned int j=0;j<data._column.size();j++){
-	MVertex *blv = data._column[j];
-	fprintf(f,"SP(%g,%g,%g){%d};\n",blv->x(),blv->y(),blv->z(),v->getNum());
+  FILE *f = Fopen("POINTS3D.pos","w");
+  if(f){
+    fprintf(f,"View \"\" {\n");
+    for (std::set<MVertex*>::iterator it = _vertices.begin(); it != _vertices.end() ; ++it){
+      MVertex *v = *it;
+      for (int i=0;i<_columns->getNbColumns(v);i++){
+        const BoundaryLayerData &data = _columns->getColumn(v,i);
+        for (unsigned int j=0;j<data._column.size();j++){
+          MVertex *blv = data._column[j];
+          fprintf(f,"SP(%g,%g,%g){%d};\n",blv->x(),blv->y(),blv->z(),v->getNum());
+        }
       }
     }
+    fprintf(f,"};\n");
+    fclose (f);
   }
-  fprintf(f,"};\n");
-  fclose (f);
-
   // END OF DEBUG STUFF
 
   return true;
diff --git a/Geo/gmshLevelset.cpp b/Geo/gmshLevelset.cpp
index 626d682cb5eb0dda912fe4f36d554f0cc8f5bb52..78beae10b627e599f83082f162de35469a63c8d9 100644
--- a/Geo/gmshLevelset.cpp
+++ b/Geo/gmshLevelset.cpp
@@ -225,13 +225,15 @@ inline double evalRadialFnDer(int p, int index, double dx, double dy, double dz,
 inline void printNodes(fullMatrix<double> &myNodes, fullMatrix<double> &surf)
 {
   FILE * xyz = Fopen("myNodes.pos","w");
-  fprintf(xyz,"View \"\"{\n");
-  for(int itv = 1; itv != myNodes.size1(); ++itv){
-    fprintf(xyz,"SP(%g,%g,%g){%g};\n", myNodes(itv,0), myNodes(itv,1),
-            myNodes(itv,2), surf(itv,0));
+  if(xyz){
+    fprintf(xyz,"View \"\"{\n");
+    for(int itv = 1; itv != myNodes.size1(); ++itv){
+      fprintf(xyz,"SP(%g,%g,%g){%g};\n", myNodes(itv,0), myNodes(itv,1),
+              myNodes(itv,2), surf(itv,0));
+    }
+    fprintf(xyz,"};\n");
+    fclose(xyz);
   }
-  fprintf(xyz,"};\n");
-  fclose(xyz);
 }
 
 // extrude a list of the primitive levelsets with a "Level-order traversal sequence"
diff --git a/Solver/elasticitySolver.cpp b/Solver/elasticitySolver.cpp
index 6e2d5ad2b7c39d6b84eaa887cd670c6b586050c6..5e5534c71935a67654a2ee662a4e0321966070b7 100644
--- a/Solver/elasticitySolver.cpp
+++ b/Solver/elasticitySolver.cpp
@@ -61,8 +61,8 @@ void elasticitySolver::setMesh(const std::string &meshFileName)
   _dim = pModel->getNumRegions() ? 3 : 2;
 
   if (LagSpace) delete LagSpace;
-  if (_dim==3) LagSpace=new VectorLagrangeFunctionSpace(_tag);
-  if (_dim==2) LagSpace=new VectorLagrangeFunctionSpace
+  if (_dim==3) LagSpace = new VectorLagrangeFunctionSpace(_tag);
+  if (_dim==2) LagSpace = new VectorLagrangeFunctionSpace
                  (_tag,VectorLagrangeFunctionSpace::VECTOR_X,
                   VectorLagrangeFunctionSpace::VECTOR_Y);
 
@@ -72,29 +72,27 @@ void elasticitySolver::setMesh(const std::string &meshFileName)
 
 void elasticitySolver::exportKb()
 {
-  FILE *f = Fopen ( "K.txt", "w" );
-  double valeur;
   std::string sysname = "A";
-  for ( int i = 0 ; i < pAssembler->sizeOfR() ; i ++ )
-  {
-    for ( int j = 0 ; j < pAssembler->sizeOfR() ; j ++ )
-    {
-      pAssembler->getLinearSystem ( sysname )->getFromMatrix ( i,j, valeur );
-      fprintf ( f,"%+e ",valeur ) ;
+  double valeur;
+  FILE *f = Fopen("K.txt", "w");
+  if(f){
+    for (int i = 0; i < pAssembler->sizeOfR(); i++){
+      for (int j = 0; j < pAssembler->sizeOfR(); j++ ){
+        pAssembler->getLinearSystem(sysname)->getFromMatrix (i, j, valeur);
+        fprintf (f, "%+e ", valeur) ;
+      }
+      fprintf(f,"\n");
     }
-    fprintf ( f,"\n" );
+    fclose (f);
   }
-
-  fclose ( f );
-
-  f = Fopen ( "b.txt", "w" );
-  for ( int i = 0 ; i < pAssembler->sizeOfR() ; i ++ )
-  {
-    pAssembler->getLinearSystem ( sysname )->getFromRightHandSide ( i,valeur );
-    fprintf ( f,"%+e\n",valeur ) ;
+  f = Fopen("b.txt", "w");
+  if(f){
+    for (int i = 0; i < pAssembler->sizeOfR(); i++){
+      pAssembler->getLinearSystem(sysname)->getFromRightHandSide(i, valeur);
+      fprintf(f, "%+e\n", valeur);
+    }
+    fclose(f);
   }
-
-  fclose ( f );
 }
 
 void elasticitySolver::solve()
@@ -147,6 +145,10 @@ void elasticitySolver::postSolve()
 void elasticitySolver::readInputFile(const std::string &fn)
 {
   FILE *f = Fopen(fn.c_str(), "r");
+  if(!f){
+    Msg::Error("Could not open file '%s'", fn.c_str());
+    return;
+  }
   char what[256];
   while(!feof(f)){
     if(fscanf(f, "%s", what) != 1){
diff --git a/Solver/multiscaleLaplace.cpp b/Solver/multiscaleLaplace.cpp
index 972b5287b7308728ebfaec0338e9b0cd9448131c..6774a3a61f07cf4a2246fd150b06c8ce11cc7fe6 100644
--- a/Solver/multiscaleLaplace.cpp
+++ b/Solver/multiscaleLaplace.cpp
@@ -632,28 +632,32 @@ static void printCut(std::map<MEdge,MVertex*,Less_Edge> &cutEdges,
    printf("Writing points.pos \n");
    std::map<MEdge,MVertex*,Less_Edge>::iterator ite = cutEdges.begin();
    FILE *f1 = Fopen("points.pos","w");
-   fprintf(f1,"View\"\"{\n");
-   for ( ; ite != cutEdges.end();++ite){
-     fprintf(f1,"SP(%g,%g,%g){1.0};\n",ite->second->x(),ite->second->y(),ite->second->z());
-   }
-   std::set<MVertex*>::iterator itv = cutVertices.begin();
-   for ( ; itv != cutVertices.end();++itv){
-     fprintf(f1,"SP(%g,%g,%g){3.0};\n",(*itv)->x(),(*itv)->y(),(*itv)->z());
+   if(f1){
+     fprintf(f1,"View\"\"{\n");
+     for ( ; ite != cutEdges.end();++ite){
+       fprintf(f1,"SP(%g,%g,%g){1.0};\n",ite->second->x(),ite->second->y(),ite->second->z());
+     }
+     std::set<MVertex*>::iterator itv = cutVertices.begin();
+     for ( ; itv != cutVertices.end();++itv){
+       fprintf(f1,"SP(%g,%g,%g){3.0};\n",(*itv)->x(),(*itv)->y(),(*itv)->z());
+     }
+     fprintf(f1,"};\n");
+     fclose(f1);
    }
-   fprintf(f1,"};\n");
-   fclose(f1);
 
    printf("Writing edges.pos \n");
    std::set<MEdge,Less_Edge>::iterator itc = theCut.begin();
    FILE *f2 = Fopen("edges.pos","w");
-   fprintf(f2,"View\"\"{\n");
-   for ( ; itc != theCut.end();++itc){
-     fprintf(f2,"SL(%g,%g,%g,%g,%g,%g){1.0,1.0};\n",itc->getVertex(0)->x(),
-             itc->getVertex(0)->y(),itc->getVertex(0)->z(),
-          itc->getVertex(1)->x(),itc->getVertex(1)->y(),itc->getVertex(1)->z());
+   if(f2){
+     fprintf(f2,"View\"\"{\n");
+     for ( ; itc != theCut.end();++itc){
+       fprintf(f2,"SL(%g,%g,%g,%g,%g,%g){1.0,1.0};\n",itc->getVertex(0)->x(),
+               itc->getVertex(0)->y(),itc->getVertex(0)->z(),
+               itc->getVertex(1)->x(),itc->getVertex(1)->y(),itc->getVertex(1)->z());
+     }
+     fprintf(f2,"};\n");
+     fclose(f2);
    }
-   fprintf(f2,"};\n");
-   fclose(f2);
 }
 
 static void printLevel(const char* fn,
@@ -671,6 +675,10 @@ static void printLevel(const char* fn,
 
   bool binary = false;
   FILE *fp = Fopen (fn, "w");
+  if(!fp){
+    Msg::Error("Could not open file '%s'", fn);
+    return;
+  }
   fprintf(fp, "$MeshFormat\n");
   fprintf(fp, "%g %d %d\n", version, binary ? 1 : 0, (int)sizeof(double));
   fprintf(fp, "$EndMeshFormat\n");