Operation FileOpen failed, error 5004 - page 2

 

This source works ok

//+------------------------------------------------------------------+
//|                                                         hhiu.mq5 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"

#property tester_file   "file.bin";

#include <Buffers.mqh>

double                  _buffer[],_series[];


CBuffers       _BUFFERS;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create timer
   EventSetTimer(60);
      
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- destroy timer
   EventKillTimer();
      
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
      MqlRates _rates[]; ArrayFree(_rates);
      int _copied=CopyRates(Symbol(),Period(),0,2,_rates);
      ArraySetAsSeries(_rates,true);

      ResetLastError();
      int _handle=FileOpen("file.bin",FILE_READ|FILE_WRITE|FILE_BIN);
      
      if(_handle!=INVALID_HANDLE){
      
         ArrayFree(_buffer); ArrayResize(_buffer,1);
         _buffer[0]=_rates[0].open;
               
         //if(_BUFFERS.SetDouble(_buffer[0],_series)) { return; }
         
         //_BUFFERS.AddDouble(_buffer[0],_series);
         
         Print(" WRITING ",
               " new value: ",_buffer[0]);
      
         FileSeek(_handle,0,SEEK_END);
         FileWriteArray(_handle,_buffer,0,1);
         FileClose(_handle);}
      else { Print(" Failed to write file, err# ",GetLastError()); }
  }

 

 

But this one does not. Why? 

//+------------------------------------------------------------------+
//|                                                         hhiu.mq5 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      "http://www.mql5.com"
#property version   "1.00"

#property tester_file   "file.bin";

#include <Buffers.mqh>

double                  _buffer[],_series[];

CBuffers       _BUFFERS;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create timer
   EventSetTimer(60);
      
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- destroy timer
   EventKillTimer();
      
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
      MqlRates _rates[]; ArrayFree(_rates);
      int _copied=CopyRates(Symbol(),Period(),0,2,_rates);
      ArraySetAsSeries(_rates,true);

      ResetLastError();
      int _handle=FileOpen("file.bin",FILE_READ|FILE_WRITE|FILE_BIN);
      
      if(_handle!=INVALID_HANDLE){
      
         ArrayFree(_buffer); ArrayResize(_buffer,1);
         _buffer[0]=_rates[0].open;
               
         if(_BUFFERS.SetDouble(_buffer[0],_series)) { return; }
         
         _BUFFERS.AddDouble(_buffer[0],_series);
         
         Print(" WRITING ",
               " new value: ",_buffer[0]);
      
         FileSeek(_handle,0,SEEK_END);
         FileWriteArray(_handle,_buffer,0,1);
         FileClose(_handle);}
      else { Print(" Failed to write file, err# ",GetLastError()); }
  }

 

 I keep getting error 5004!!

 

I have a same problem:

 

My EA, at the end of the testing session, write some values in a Report File.csv
After each new test, I must to append new result in my report file.
All it work fine until it need to recompile the EA.
After recompilation, if I run a new test the Report File.csv will be deleted by Tester Strategy Agent.

 This is my code, but it is not related with:

void salvareRezultat()
{
   string rezultat = Val1+";"+Val2+";"+nrPos+";"+NewComment;
   string path = MQL5InfoString(MQL5_PROGRAM_NAME)+"__"+ Symbol();
   path=path+".csv";
   
   int handle=-1;

   handle = FileOpen(path,FILE_READ|FILE_WRITE);
        FileSeek(handle,0,SEEK_END);
        FileWrite(handle,rezultat);
        FileFlush(handle);
        FileClose(handle);

}
Reason: