File read - File write - Acting weird in a binary file

 

I created a function that could read and write to a binary file, 

bool CNeuralNetwork::Bin(double &Arr1[],string flag)
 {
   int handle_w=0, handle_r, int_flag = 10;
   
   if (flag == "read") int_flag = 0; else if (flag == "write") int_flag=1;
   
   switch(int_flag)
     {
      case  0:
      
           handle_r = FileOpen("NN EA"+"\\modelbin.bin",FILE_READ|FILE_BIN); 
           if (handle_r == INVALID_HANDLE)           printf("Invalid Read binary handle Err =%d ",GetLastError());
           else  {
                   FileReadArray(handle_r,Arr1);            
                   return(true);  
                   
                    FileClose(handle_r);
                 }
        break;
        
        
      case  1:
           handle_w = FileOpen("NN EA"+"\\modelbin.bin",FILE_WRITE|FILE_BIN); 
           if (handle_w == INVALID_HANDLE) printf("Invalid Write binary handle Err =%d ",GetLastError());
           else  { 
                   FileWriteArray(handle_w,Arr1);            
                   return(true);  
                                 
                    FileClose(handle_w);
                 }
        break;
        
      default:
        Print("Unknown file handle flag");
        break;
     }  
      
   
   return(false);
 }


This code does work in one situation only, when I call this  function to write  to a binary file, it does work however I am getting Err 5004 which if file opening error when I call this to read 

the same thing happens when I call the function to write to a binary file first, It no longer allows me to read the file when I decide to call the function to write the next time in the program runtime,

It also appears that the MT5 platform keeps the file opened with the handle before open until you close the platform, that way you can not open the same file with another handle (I'm not sure), because when I try to delete the file in windows files I am told I can't because it is being used by MT5 even though I closed the handle at the end of my operation as you can see in the above code, someone help to get this working 

Documentation on MQL5: File Functions / FileOpen
Documentation on MQL5: File Functions / FileOpen
  • www.mql5.com
FileOpen - File Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

I figured out that nothing was wrong in the code except that I was calling the function twice in one loop so the whole process of opening and closing the handles becomes ineffective due to loop execution speed leading to one handle to not be closed whilst the other handle for the same file gets opened

The same thing that happens when you open and close the file on the Ontick function, because the execution is super fast the FileClose and FileOpen seems to not be able to catch up so you will definitely get err 5004 when you attempt to perform such operation inside the ontick(On every tick)

This is common to files because from what I know is that you can not open and use one file in multiple programs at the same time you will get the same error 5004, it is the same thing that happens when trying to delete a file that is in use by the other program

hope you guys will avoid such situation

best regards

 
Omega J Msigwa #: I figured out that nothing was wrong in the code
Don't use strings or ints when you mean an enumeration
bool CNeuralNetwork::Bin(double &Arr1[],string flag)
 {
   int handle_w=0, handle_r, int_flag = 10;
   
   if (flag == "read") int_flag = 0; else if (flag == "write") int_flag=1;
   
   switch(int_flag)
     {
      case  0:
Write self-documenting code.
enum FileMode{ reading, writing );
⋮
bool CNeuralNetwork::Bin(double &Arr1[],FileMode mode)
 {
   int handle_w=0, handle_r;
   
   switch(mode)
     {
      case  reading:
 
Omega J Msigwa #:

I figured out that nothing was wrong in the code except that I was calling the function twice in one loop so the whole process of opening and closing the handles becomes ineffective due to loop execution speed leading to one handle to not be closed whilst the other handle for the same file gets opened

The same thing that happens when you open and close the file on the Ontick function, because the execution is super fast the FileClose and FileOpen seems to not be able to catch up so you will definitely get err 5004 when you attempt to perform such operation inside the ontick(On every tick)

This is common to files because from what I know is that you can not open and use one file in multiple programs at the same time you will get the same error 5004, it is the same thing that happens when trying to delete a file that is in use by the other program

hope you guys will avoid such situation

best regards

At first glance your return statements are before your file close statements so I suspect that could be why your files remain open - change the sequence and try again

case  0:
      
           handle_r = FileOpen("NN EA"+"\\modelbin.bin",FILE_READ|FILE_BIN); 
           if (handle_r == INVALID_HANDLE)           printf("Invalid Read binary handle Err =%d ",GetLastError());
           else  {
                   FileReadArray(handle_r,Arr1);            
                   return(true);  
                   
                    FileClose(handle_r);
                 }
        break;
 
                   FileWriteArray(handle_w,Arr1);            
                   return(true);  
                                 
                    FileClose(handle_w);

You return without closing the file. All reads now fail.

 
Besides the coding flaws. You should not want to do file operations on every tick for any reason.
 
Thank you all the suggestions were helpful