Problem with saving files

 

I have this code for saving tickets that are stored in an object of the CArrayLong class


void Save(void)
  {
   if(m_tester || m_optimization)
      return;
//---
   string name = "";
   if(!GenerateFileName(name))
      return;
   if(m_basket.Total() == 0)
     {
      if(FileIsExist(name))
         FileDelete(name);
     }
   else
     {
      int handle = FileOpen(name,FILE_WRITE|FILE_BIN);
      if(handle != INVALID_HANDLE)
        {
         ResetLastError();
         int ms = 100;
         Sleep(ms);
         //---
         m_basket.Save(handle);
         if(GetLastError() != 0)
            printf("Error on line %d",__LINE__);
         Sleep(ms);
         //---
         FileFlush(handle);
         if(GetLastError() != 0)
            printf("Error on line %d",__LINE__);
         Sleep(ms);
         //---
         FileClose(handle);
         if(GetLastError() != 0)
            printf("Error on line %d",__LINE__);
         Sleep(ms);
         //---
         printf("Saved successfully | %s",name);
        }
      else
         printf(__FUNCTION__" file opening failed %d",GetLastError());
     }
  }


If I run the code in debug mode, it works perfectly and the file is saved. However, if I run it live, I still see the message "Saved successfully ..." and even though there are no errors, the file is not created in the system. 


When compiling the program, I have tried using "No optimizations" and it has no effect. Am I missing something or is this an MQL5 bug?

 

For more context, I'll add the declarations of the variables used.


CArrrayLong m_basket;
bool m_tester;
bool m_optimization;


An example of the return value from the GenerateFileName function - "Prefix_EURUSD_9061_0.bin"

 
Victor Eze #:

For more context, I'll add the declarations of the variables used.



An example of the return value from the GenerateFileName function - "Prefix_EURUSD_9061_0.bin"

Are you sure the file is not accidentally being deleted with this?

if(m_basket.Total() == 0)
     {
      if(FileIsExist(name))
         FileDelete(name);
     }

I would suggest instead of deleting, rename to a unique filename 

That will reveal if this is this the case

 
R4tna C #:

Are you sure the file is not accidentally being deleted with this?

I would suggest instead of deleting, rename to a unique filename 

That will reveal if this is this the case

Thanks, that's an excellent suggestion.


Although the file was not deleted from this line. I found there was a different section of the code that would delete the file once it was created.

 
Victor Eze #:

Thanks, that's an excellent suggestion.


Although the file was not deleted from this line. I found there was a different section of the code that would delete the file once it was created.

Good stuff 

 
Victor Eze #:

Thanks, that's an excellent suggestion.


Although the file was not deleted from this line. I found there was a different section of the code that would delete the file once it was created.

I'm not yet as good at programming as some people here, but to avoid this type of error I try to create prints almost everywhere,


if(FileIsExist(name)) {
      FileDelete(name);
      Print("Somethink");
}
 
ZeroCafeine #:

I'm not yet as good at programming as some people here, but to avoid this type of error I try to create prints almost everywhere,


A classic technique - and still one of the best!

 
ZeroCafeine #:

I'm not yet as good at programming as some people here, but to avoid this type of error I try to create prints almost everywhere,


Yes, works very well. 

To make the prints more manageable, I'll recommend encasing them in an if statement.

bool debug = true; //global variable

void CSomeClass::SomeFunction()
{
 if(FileIsExist(name)) {
 FileDelete(name);
 if(debug)
        printf(__FUNCTION__" file %s deleted",name); 
 }
}