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

uint  FileReadStruct(
   int          file_handle,        // file handle
   const void&  struct_object,      // target structure to which the contents are read
   int          size=-1             // structure size in bytes



[in] File descriptor of an open bin-file.


[out]  The object of this structure. The structure should not contain strings, dynamic arrays or virtual functions.


[in]  Number of bytes that should be read. If size is not specified or the indicated value is greater than the size of the structure, the exact size of the specified structure is used.

Return Value

If successful the function returns the number of bytes read. File pointer is moved by the same number of bytes.

Example (the file obtained after using the example for FileWriteStruct function is used here)

//|                                          Demo_FileReadStruct.mq5 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                     |
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      ""
#property version   "1.00"
#property indicator_separate_window
#property indicator_buffers 4
#property indicator_plots   1
//---- plot Label1
#property indicator_label1  "Candles"
#property indicator_type1   DRAW_CANDLES
#property indicator_color1  clrOrange
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
#property indicator_separate_window
//--- parameters for receiving data
input string  InpFileName="EURUSD.txt"// file name
input string  InpDirectoryName="Data";  // directory name
//| Structure for storing candlestick data                           |
struct candlesticks
   double            open;  // open price
   double            close; // close price
   double            high;  // high price
   double            low;   // low price
   datetime          date;  // date
//--- indicator buffers
double       open_buff[];
double       close_buff[];
double       high_buff[];
double       low_buff[];
//--- global variables
candlesticks cand_buff[];
int          size=0;
int          ind=0;
//| Custom indicator initialization function                         |
int OnInit()
   int default_size=100;
//--- open the file
   int file_handle=FileOpen(InpDirectoryName+"//"+InpFileName,FILE_READ|FILE_BIN|FILE_COMMON);
      PrintFormat("%s file is available for reading",InpFileName);
      PrintFormat("File path: %s\\Files\\",TerminalInfoString(TERMINAL_COMMONDATA_PATH));
      //--- read data from the file
         //--- write data to the array
         //--- check if the array is overflown
            //--- increase the array size
      //--- close the file
      PrintFormat("Data is read, %s file is closed",InpFileName);
      PrintFormat("Failed to open %s file, Error code = %d",InpFileName,GetLastError());
//--- indicator buffers mapping
//--- empty value
//| Custom indicator iteration function                              |
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
//--- the loop for the candlesticks that have not been handled yet
   for(int i=prev_calculated;i<rates_total;i++)
      //--- 0 by default
      //--- check if any data is still present
         for(int j=ind;j<size;j++)
            //--- if dates coincide, the value from the file is used
               //--- increase the counter
//--- return value of prev_calculated for next call

See also

Structures and classes, FileWriteStruct