Need help with FileReadStruct function


I'm using the FileReadStruct function to read and copy data from a CSV file. However, the algo returns the 'array out of range' error (see execution_report file). I'm following the documentation available on

The CSV contains three columns (file attached to this post), with the first and the second column being datetime type and the third being double type.

Any idea on how to solve this issue?


//--- global variables
input string inpFileName = "testStruct.csv";
//--- structure to store dividend payments and dates data    
struct prices
   datetime    dataCom;
   datetime    dataPgto;
   double      dividend;   
//--- global array that will store the data from the file
prices arr[];

int OnInit(void)

void open_file()
//--- working in the "file sandbox"
   int n = 0;  
   int defaultSize = 5; 
   int fileHandle;
   fileHandle=FileOpen(inpFileName,FILE_READ|FILE_CSV|FILE_ANSI|FILE_COMMON, ";", CP_ACP);
      PrintFormat("%s file is available for reading", inpFileName);
      PrintFormat("File path: %s\\Files\\",TerminalInfoString(TERMINAL_COMMONDATA_PATH));
      //--- read data from the file
      ArrayResize(arr, defaultSize);
         //--- write data to the array
         FileReadStruct(fileHandle, arr[n]);
      //--- close the file
      PrintFormat("Data is read, %s file is closed",inpFileName);
      //--- print structure arr          
      for(int i=0;i<ArraySize(arr);i++){
         Print(arr[i].dataCom, " ",  arr[i].dataPgto , " ", arr[i].dividend);
      PrintFormat("Failed to open %s file, Error code = %d", inpFileName, GetLastError());
Documentation on MQL5: File Functions / FileReadStruct
Documentation on MQL5: File Functions / FileReadStruct
FileReadStruct - File Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5

That function is for reading binary data. It should not be used for textual files like CSV.


The function reads contents into a structure passed as a parameter from a binary-file, starting with the current position of the file pointer.