Need help with FileReadStruct function

 
Hello,


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 https://www.mql5.com/en/docs/files/filereadstruct

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?

Cheers!

//--- 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)
  {
   open_file();
   //---   
   return(INIT_SUCCEEDED);
  }


void open_file()
  {   
//--- working in the "file sandbox"
   int n = 0;  
   int defaultSize = 5; 
   ResetLastError();
   int fileHandle;
   fileHandle=FileOpen(inpFileName,FILE_READ|FILE_CSV|FILE_ANSI|FILE_COMMON, ";", CP_ACP);
   if(fileHandle!=INVALID_HANDLE)
     {
      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);
      while(!FileIsEnding(fileHandle))
        {
         //--- write data to the array
         FileReadStruct(fileHandle, arr[n]);
         n++;
        }
          
      //--- close the file
      FileClose(fileHandle);
      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);
      }
      
     }
   else
      PrintFormat("Failed to open %s file, Error code = %d", inpFileName, GetLastError());
  }
Documentation on MQL5: File Functions / FileReadStruct
Documentation on MQL5: File Functions / FileReadStruct
  • www.mql5.com
FileReadStruct - File Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
Files:
 

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

FileReadStruct

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