Too Many Opened Files - Error 5001

 

Hello everyone, I've been struggling with the error for some time now, and I can't seem to make it work. 

I'd appreciate if someone can help.

This is my code:

void Writer(string body, string filename, bool ShouldBeEmptied){

   int handle;      
   while(true){
      if(ShouldBeEmptied){      
         handle = FileOpen("/USDJPY/" + filename + ".txt", FILE_WRITE|FILE_SHARE_READ|FILE_COMMON);
         if(handle == INVALID_HANDLE){ 
            Alert("Failed to empty the file" + GetLastError() + filename);
         }
         FileClose(handle);        
      }
      
      //This is for writing
      handle = FileOpen("/USDJPY/" + filename + ".txt", FILE_READ|FILE_WRITE|FILE_TXT|FILE_COMMON|FILE_SHARE_READ|FILE_SHARE_WRITE);
      if(handle == INVALID_HANDLE) {
         Alert("Failed to create file" + GetLastError());
      }
      FileSeek(handle, 0, SEEK_END);
      FileWrite(handle, body);
      FileClose(handle);
      break;   
   }

}


Obviously, it is writing function where at first clears the file and then writes to it.

The reason I'm putting it in while loop is to make sure it writes to the file since other EAs could be writing to the file in the same time. 

 

5001

ERR_FILE_TOO_MANY_OPENED

Too many opened files

Runtime Errors - Codes of Errors and Warnings - Constants, Enumerations and Structures - MQL4 Reference
Runtime Errors - Codes of Errors and Warnings - Constants, Enumerations and Structures - MQL4 Reference
  • docs.mql4.com
Runtime Errors - Codes of Errors and Warnings - Constants, Enumerations and Structures - MQL4 Reference
 
Fernando Carreiro #:

5001

ERR_FILE_TOO_MANY_OPENED

Too many opened files

 
Obviously I looked at this before but still can’t figure out why it is being triggered. As you can tell from the code, every handler I open, I close afterward to avoid opening too many files, am I right?
 
Mansour Fahad M Almogaiteeb #: As you can tell from the code, every handler I open, I close afterward to avoid opening too many files, am I right?

Analyse your code again. You are still processing the file even when the handle is invalid.

 

Uncompiled, untested code. Simply typed out.

void Writer(string body, string filename, bool ShouldBeEmptied){

   int handle;
   string filepath = "/USDJPY/" + filename + ".txt";

   while(true) {
      if(ShouldBeEmptied) {      
         handle = FileOpen(filepath, FILE_WRITE|FILE_SHARE_READ|FILE_COMMON);
         if( handle != INVALID_HANDLE )
            FileClose(handle);
         else 
            Alert("Failed to empty the file" + GetLastError() + filename);
      }
         
      //This is for writing
      handle = FileOpen(filepath, FILE_READ|FILE_WRITE|FILE_TXT|FILE_COMMON|FILE_SHARE_READ|FILE_SHARE_WRITE);
      if( handle != INVALID_HANDLE ) {
         FileSeek(handle, 0, SEEK_END);
         FileWrite(handle, body);
         FileClose(handle);
         break;
      } else
         Alert("Failed to create file" + GetLastError());
   }
}
 

However, I still consider your code logic flawed. The code above is just to show that you were still processing the file even when the handle was invalid.

 
Fernando Carreiro #:

However, I still consider your code logic flawed. The code above is just to show that you were still processing the file even when the handle was invalid.

Thank you Fer,

I tested your code and unfortunately the same error still pops-up. However, I did a small modification which eventually fixed the issue. 

I removed the alert method from the code; hence, the code will fail silently. It seems the constant calling for the Alert() method creates a bit of lag in the processing which prevents the FileClose() method from being called.

Now the issue is resolved.