"too many opened files" error

 

Hi,

Please help me to find out why these codes get "too many opened files" error.

 
...
 
    for (Counter=0;Counter<OrdersTotal();Counter++)
     { OrderSelect(Counter, SELECT_BY_POS, MODE_TRADES);
       if (OrderOpenTime()>New_Order_Time)
        { New_Order_Time=OrderOpenTime();
          Open_Filing();
        }
     }
...
 
  int Open_Filing()
  { Handle_Open=FileOpen("open.csv", FILE_CSV|FILE_READ, ';');
    if (Handle_Open<0)
      { Handle_Open=FileOpen("open.csv", FILE_CSV|FILE_WRITE, ';');
        Handle_Open=FileOpen("open.csv", FILE_CSV|FILE_READ, ';');
      }
 
    if(Handle_Open>0)
      { Handle_Open=FileOpen("open.csv", FILE_CSV|FILE_WRITE, ';');
        FileWrite(Handle_Open,OrderType(),OrderOpenTime(),OrderSymbol(),OrderOpenPrice(),OrderStopLoss(),OrderTakeProfit());
        FileClose(Handle_Open);
      }
    return(0);
  }

I have put 
FileClose(Handle_Open); after filewrite(), why too many files are still open?

Thanks!

 

Hi Dance,

you must close the files after using them, otherwise they stay open and therefore the error.

Good luck in the trading.

 
forexfordinner:

Hi Dance,

you must close the files after using them, otherwise they stay open and therefore the error.

Good luck in the trading.

Hi, forexfordinner

Thanks for help!

But I have put FileClose() after every filewrite(), seems not good either.

Happy trading!

 
"But I have put FileClose() after every filewrite(), seems not good either."

Correct, you have - but: you call fileopen in function a maximum of 3 times

and only have one close.

.

Please read mql book about files https://book.mql4.com/functions/files

.

MetaEditor docs on int FileOpen(..)

Notes: Files can only be opened in the terminal_directory\experts\files folder (terminal_directory\tester\files if for expert testing) or in its subfolders.
FILE_BIN and FILE_CSV modes cannot be used simultaneously.
If FILE_WRITE does not combine with FILE_READ, a zero-length file will be opened. If even the file containd some data, they will be deleted. If there is a need to add data to an existing file, it must be opened using combination of FILE_READ | FILE_WRITE.
If FILE_READ does not combine with FILE_WRITE, the file will be opened only if it already exists. If the file does not exist, it can be created using the FILE_WRITE mode.
No more than 32 files can be opened within an executable module simultaneously. Handles of files opened in the same module cannot be passed to other modules (libraries).

Every open must have close - else you end up as in your case with a possible three opens and one close - meaning each time function called the worst case is that when function exits two file handles remain used/comsumed/allocated and can only have maximum 32 files open at anyone time.

Yes... you open 'one' filename but you keep opening this file and the system is doing exactely what you asked it to do until it 32 opens have been done then it will issue the error

Please read the mql4 book - you must understand how to use the available tools and understand the results of using them too!

enjoy the trip

;)

 
ukt:
"But I have put FileClose() after every filewrite(), seems not good either."

Correct, you have - but: you call fileopen in function a maximum of 3 times

and only have one close.

.

Please read mql book about files https://book.mql4.com/functions/files

.

MetaEditor docs on int FileOpen(..)

Notes: Files can only be opened in the terminal_directory\experts\files folder (terminal_directory\tester\files if for expert testing) or in its subfolders.
FILE_BIN and FILE_CSV modes cannot be used simultaneously.
If FILE_WRITE does not combine with FILE_READ, a zero-length file will be opened. If even the file containd some data, they will be deleted. If there is a need to add data to an existing file, it must be opened using combination of FILE_READ | FILE_WRITE.
If FILE_READ does not combine with FILE_WRITE, the file will be opened only if it already exists. If the file does not exist, it can be created using the FILE_WRITE mode.
No more than 32 files can be opened within an executable module simultaneously. Handles of files opened in the same module cannot be passed to other modules (libraries).

Every open must have close - else you end up as in your case with a possible three opens and one close - meaning each time function called the worst case is that when function exits two file handles remain used/comsumed/allocated and can only have maximum 32 files open at anyone time.

Yes... you open 'one' filename but you keep opening this file and the system is doing exactely what you asked it to do until it 32 opens have been done then it will issue the error

Please read the mql4 book - you must understand how to use the available tools and understand the results of using them too!

enjoy the trip

;)

Oh, I see!

Thanks! Thanks a lot! Thank you very kindly!

Happy trading!

Reason: