Error Code 5004 - page 3

 
RaptorUK:

There was no file attached . . .

Have you confirmed that the file you are trying to read from actually exists ? please look in the correct folder and check . . . one other thing, why do you have a function called filewriting() to READ from a file ?


Dear RaptorUK,

Yes, I can confirm that the DAT file is in the folder MQL4/files. In fact, I have informed that I put a condition whether the fileopen succeeds and it succeeds most of the time (90%) and 10% fails to open and generate a 5004 error code.

The filewriting() function is called when a fileopen fails. Then, the funcition filewriting would try to create a file (open for write) and at the end of the function, do the fileclose.

I have attached the file from the Print() statement out of MQL4\logs folder. I clicked the button "Choose File" and I could see that the file name was displayed.

Warm regards,

Raisingfire

Files:
 
raisingfire:


Dear RaptorUK,

Yes, I can confirm that the DAT file is in the folder MQL4/files. In fact, I have informed that I put a condition whether the fileopen succeeds and it succeeds most of the time (90%) and 10% fails to open and generate a 5004 error code.

The filewriting() function is called when a fileopen fails. Then, the funcition filewriting would try to create a file (open for write) and at the end of the function, do the fileclose.

I have attached the file from the Print() statement out of MQL4\logs folder. I clicked the button "Choose File" and I could see that the file name was displayed.

Warm regards,

Raisingfire

Where do these files come from ? what writes them ?
 
RaptorUK:
Where do these files come from ? what writes them ?

Dear RaptorUK,

All the DAT files are meant to be a temporary files to hold initial values of certain indicators every time we open a position. We created and used them in our programs.

We use standard function to open the file for writing:

fhandle=FileOpen(mf, FILE_BIN|FILE_WRITE);

Standard array writing to the temp files (DAT) :

FileWriteArray(fhandle, mpara, 0, maxori); // where mpara is the array, Maxori is the maximum size of the array

Standard file closing function:

FileClose(fhandle);

Thank you for your help.


Regards,

Raisingfire

 
raisingfire:

Dear RaptorUK,

All the DAT files are meant to be a temporary files to hold initial values of certain indicators every time we open a position. We created and used them in our programs.

We use standard function to open the file for writing:

fhandle=FileOpen(mf, FILE_BIN|FILE_WRITE);

Standard array writing to the temp files (DAT) :

FileWriteArray(fhandle, mpara, 0, maxori); // where mpara is the array, Maxori is the maximum size of the array

Standard file closing function:

FileClose(fhandle);


OK, but you didn't actually answer my question . . . are they written by the same EA that is trying to read them ? or a different EA, Script or Indicator ?
 
RaptorUK:

OK, but you didn't actually answer my question . . . are they written by the same EA that is trying to read them ? or a different EA, Script or Indicator ?


The Filewriting function is in all in the same EA. Everytime, we issue a "FILEOPEN", we always follow it by "FILECLOSE" after we do certain task with it. So, I don't think there is a problem with locked file that is being tried to be opened.

Long time ago, we had that kind of problem where a locked file was being tried to be read. And MT4 automatically reported it in its log file. So, after 1 or 2 days, the Logs and history folders became huge in size (hundreds of Mbytes), full of those reports. Then, we issue a FILECLOSE everytime we do FILEOPEN. The problem was solved and the logs and history folders never got huge anymore. Now, the log and history folders combined contains only less than 100 Kbytes (despite the error code 5004 that keeps popping up).


Thanks,

Raisingfire

 
raisingfire:

The Filewriting function is in all in the same EA. Everytime, we issue a "FILEOPEN", we always follow it by "FILECLOSE" after we do certain task with it. So, I don't think there is a problem with locked file that is being tried to be opened.

Long time ago, we had that kind of problem where a locked file was being tried to be read. And MT4 automatically reported it in its log file. So, after 1 or 2 days, the Logs and history folders became huge in size (hundreds of Mbytes), full of those reports. Then, we issue a FILECLOSE everytime we do FILEOPEN. The problem was solved and the logs and history folders never got huge anymore. Now, the log and history folders combined contains only less than 100 Kbytes (despite the error code 5004 that keeps popping up).

OK thanks for the information. I'm out of ideas . . . all I can suggest now is to create a test EA that reproduces the issue that you can post here and send to MetaQuotes as part of a Service Desk ticket and ask them to investigate.
 
RaptorUK:
OK thanks for the information. I'm out of ideas . . . all I can suggest now is to create a test EA that reproduces the issue that you can post here and send to MetaQuotes as part of a Service Desk ticket and ask them to investigate.


Dear RaptorUK,


Thank you for all you help and info anyway. I will try to do what you told me.

Once again, Thank you.


Regards,

 
//+------------------------------------------------------------------+
//| expert count strategy open position function  (closedPos)          |
//+------------------------------------------------------------------+
void closedPos() {  

   int file=FileOpen("hist0935.csv",FILE_WRITE|FILE_CSV);
   
  
      
for(int i=OrdersHistoryTotal()-1; i>=0; i--) {

int file=FileOpen("hist0935.csv",FILE_READ|FILE_WRITE|FILE_CSV);

ResetLastError();
file=FileOpen("hist0935.csv",FILE_READ|FILE_WRITE|FILE_CSV);


      OrderSelect(i, SELECT_BY_POS, MODE_HISTORY); 

  if (file != INVALID_HANDLE){    

   PrintFormat("%s file is available for writing",file);
  
  
     
      FileWrite(file,OrderTicket(),OrderClosePrice(),OrderCloseTime());
      
      //--- close the file
     Print("error="+GetLastError());
      FileClose(file);
      PrintFormat("%s file is closed",file);
       
  }else{
      Alert("hist0935 Events File Error.",GetLastError());
   }
  
   }        
 // return();
}
 
raisingfire:

Dear RaptorUK,


Thank you for all you help and info anyway. I will try to do what you told me.  

Once again, Thank you.


Regards,



samoye:

   Is there a solution to error 5004 please? I am getting the same error message trying to write to a file.  

//+------------------------------------------------------------------+
//| expert count strategy open position function  (openPos)          |
//+------------------------------------------------------------------+
void closedPos() {  

   int file=FileOpen("hist0935.csv",FILE_WRITE|FILE_CSV);
   
  
      
for(int i=OrdersHistoryTotal()-1; i>=0; i--) {

int file=FileOpen("hist0935.csv",FILE_READ|FILE_WRITE|FILE_CSV);

ResetLastError();
file=FileOpen("hist0935.csv",FILE_READ|FILE_WRITE|FILE_CSV);


      OrderSelect(i, SELECT_BY_POS, MODE_HISTORY); 
//Print("OrderClosePrice()= " +OrderClosePrice()+"OrderCloseTime()= " + OrderCloseTime()+"OrderComment()= " + OrderComment()+ "OrderCommission()= " +OrderCommission()+ "OrderExpiration()= " +OrderExpiration()+ "OrderLots()= " +OrderLots()+ "OrderMagicNumber()= " +OrderMagicNumber()+ "OrderOpenPrice()= " +OrderOpenPrice()+ "OrderOpenTime()= " +OrderOpenTime()+ "OrderProfit()= " +OrderProfit()+ "OrderStopLoss()= " +OrderStopLoss()+ "OrderSwap()= " +OrderSwap()+ "OrderSymbol()= " +OrderSymbol()+ "OrderTakeProfit()= " +OrderTakeProfit()+ "OrderTicket()= " +OrderTicket()+ "OrderType()= " +OrderType() ); 
 

  if (file != INVALID_HANDLE){    

   PrintFormat("%s file is available for writing",file);
  
   
      //FileWrite(file,OrderClosePrice(),OrderCloseTime(), OrderComment(),OrderCommission(),OrderExpiration(),OrderLots(),OrderMagicNumber(),OrderOpenPrice(),OrderOpenTime(),OrderProfit(),OrderStopLoss(),OrderSwap(),OrderSymbol(),OrderTakeProfit(),OrderTicket(),OrderType());
     
      FileWrite(file,OrderTicket(),OrderClosePrice(),OrderCloseTime());
      
      //--- close the file
     Print("error="+GetLastError());
      FileClose(file);
      PrintFormat("%s file is closed",file);
       
  }else{
      Alert("hist0935 Events File Error.",GetLastError());
   }
  
   }        
 // return();
}

I am on build 765.

Your  help is greatly appreciated. 

 
   int file=FileOpen("hist0935.csv",FILE_WRITE|FILE_CSV);  
      
for(int i=OrdersHistoryTotal()-1; i>=0; i--) {

int file=FileOpen("hist0935.csv",FILE_READ|FILE_WRITE|FILE_CSV);
                                                                
ResetLastError();                                               
file=FileOpen("hist0935.csv",FILE_READ|FILE_WRITE|FILE_CSV);    

  1. You open the file for writing, it is now zero length.
  2. Begin loop
  3. You open the file for reading and writing. You now have the file open twice and have lost the original handle.
  4. ResetLastError unnecessary, only look at GetLastError, when you have a problem
  5. You open the file thrice and have lost two handles.
  6. Next loop you open the file more times.
Reason: