0
votes

I am trying to read a file into structure, but failed as there was a compilation error. See what I tried:

struct file_row_struct
{
   datetime file_time;
   string file_range_green;
   string file_range_red;
   double file_dist_green_red;
   double file_slope_green;
   double file_slope_red;
   string file_prev_color;
   string file_current_color;   
}filerow[];

int size = 1;
FileReader = FileOpen(file_read_path,FILE_READ|FILE_CSV,','); 
   if(FileReader != INVALID_HANDLE)
   {
   //while(!FileIsEnding(FileReader))
   //   linecount++;
   while(!FileIsEnding(FileReader))
      {
         FileReadStruct(FileReader,filerow,size); 
         size++; 

      }   
   Print("File Opened successfully");
   //PrintFormat("File path: %s\\Files\\",TerminalInfoString(TERMINAL_DATA_PATH));
   FileClose(FileReader);
   }
   else Print("Not Successful in opening file:  %s  ", GetLastError());

The gist of sample file is available at: Sample data

The compilation error that I encountered is as follows:

'filerow' - structures containing objects are not allowed   NeuralExpert.mq5    108 36

Kindly, suggest me what I have mistaken. My guess is that there is an availability of the string member function in the structure, hence it is not allowing.

1

1 Answers

1
votes

Structures are simple types in MQL. That means you can have integer and floating values of all kinds in it (anything that casts to ulong and double) and some others. That also means you cannot have strings and other structures in it. If you have strings in the structure - you cannot pass by reference and many other problems (so it is better to say complex types are not supported in structures, you still may have them but it is your responsibility to do everything correctly).
Since you cannot pass structures by reference, you cannot use FileReadStruct().
What to do - I would suggest use of a CObject-based class and CArrayObj to hold them instead of filerow[].

class CFileRow : public CObject
   {
//8 fields
public:
    CFileRow(const string line)
      {
      //convert string line that you are to read from file into class
      }
    ~CFileRow(){}
   };
CArrayObj* fileRowArray = new CArrayObj();

while(!FileIsEnding(FileReader))
  {
     string line=FileReadString(FileReader);
     fileRowArray.Add(new CFileRow(line));
  }