MQL4参考 文件函数 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.

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 or 0 in case of error. If successful, the number of bytes read corresponds to the size of the structure. To obtain information about the error call the GetLastError() function. 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.mq4 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                     |
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      ""
#property version   "1.00"
#property indicator_separate_window
#property indicator_buffers 4
//---- plot Label1
#property indicator_label1  "Open"
#property indicator_type1    DRAW_LINE
#property indicator_color1  clrBlue
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
#property indicator_label1  "High"
#property indicator_type2    DRAW_LINE
#property indicator_color2  clrGreen
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
#property indicator_label1  "Low"
#property indicator_type3    DRAW_LINE
#property indicator_color3  clrOrange
#property indicator_style3  STYLE_SOLID
#property indicator_width3  1
#property indicator_label1  "Close"
#property indicator_type4    DRAW_LINE
#property indicator_color4  clrRed
#property indicator_style4  STYLE_SOLID
#property indicator_width4  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
         uint bytesread=FileReadStruct(file_handle,cand_buff[size]);        
         //--- check data read
         if (bytesread!=sizeof(candlesticks))
            PrintFormat("Error reading data. Error code=%d",GetLastError());
            //--- close the file
            //--- 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()