Array struct problem with string value?

 

Hi,


I used char in symbol but I got value=0, If use string then I got combile Error: 'arr' - structures or classes containing objects are not allowed testtoarray.mq4 68 33




struct prices
  {
   char          symbol; // symbol
   double            price;  // price
  };
//--- global variables
int    count=0;
int    size=20;
prices arr[];
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- allocate memory for the array
   ArrayResize(arr,size);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- write the remaining count strings if count<n
   WriteData(count);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
string sym="test//test.bin";
void OnTick()
  {
//--- save data to array
   arr[count].symbol="EURUSD";
   arr[count].price="1.087";
//--- show current data
   count++;
//--- if the array is filled, write data to the file and zero it out
   if(count==size)
     {
     // WriteData(size);
      count=0;
     }
     
      prices arr[];
//--- file path

//--- open the file
   ResetLastError();
   int file_handle=FileOpen(sym,FILE_READ|FILE_BIN|FILE_COMMON);
   if(file_handle!=INVALID_HANDLE)
     {
      //--- read all data from the file to the array
      FileReadArray(file_handle,arr);
      //--- receive the array size
      int size=ArraySize(arr);
      //--- print data from the array
      for(int i=0;i<size;i++)
         Print("Symbol = ",arr[i].symbol," Price = ",arr[i].price);
    //  Print("Total data = ",size);
      //--- close the file
      FileClose(file_handle);
     }
   else
      Print("File open failed, error ",GetLastError());
     
     
     
  }
//+------------------------------------------------------------------+
//| Write n elements of the array to file                            |
//+------------------------------------------------------------------+
void WriteData(const int n)
  {   
//--- open the file
   ResetLastError();
   int handle=FileOpen(sym,FILE_READ|FILE_WRITE|FILE_COMMON|FILE_BIN);
   if(handle!=INVALID_HANDLE)
     {
      //--- write array data to the end of the file
      FileSeek(handle,0,SEEK_END);
      FileWriteArray(handle,arr,0,n);
      //--- close the file
      FileClose(handle);
     }
   else
      Print("Failed to open the file, error ",GetLastError());
  }
 
Dejan Krapez:

Hi,


I used char in symbol but I got value=0, If use string then I got combile Error: 'arr' - structures or classes containing objects are not allowed testtoarray.mq4 68 33


You cannot store strings in a char.

You can have a string in a structure.

highlight the line where you get the error.

 
Paul Anscombe #:

You cannot store strings in a char.

You can have a string in a structure.

highlight the line where you get the error.

Here I got error:

      FileReadArray(file_handle,arr);


'arr' - structures or classes containing objects are not allowed testtoarray.mq4 68 33

AND


here:

      FileWriteArray(handle,arr,0,n);

 
Dejan Krapez #:

Here I got error:

      FileReadArray(file_handle,arr);


'arr' - structures or classes containing objects are not allowed testtoarray.mq4 68 33

AND


here:

      FileWriteArray(handle,arr,0,n);

you cannot write an array of structure data with FileWriteArray

there is a FileWriteStructure but the structure cannot contain strings.

You will need to change your data model and the way you save/read from files accordingly.

 
Dejan Krapez:

Hi,


I used char in symbol but I got value=0, If use string then I got combile Error: 'arr' - structures or classes containing objects are not allowed testtoarray.mq4 68 33




struct prices
  {
   char              symbol[16]; // symbol
   double            price;  // price
  };

StringToCharArray("EURUSD",arr[count].symbol);

CharArrayToString(arr[i].symbol)

You need a fixed length byte array.