MQL4参考 文件函数 FileReadInteger


The function reads int, short or char value from the current position of the file pointer depending on the length specified in bytes.

int  FileReadInteger(
   int  file_handle,        // File handle
   int  size=INT_VALUE      // Size of an integer in bytes



[in]  File descriptor returned by FileOpen().


[in]  Number of bytes (up to 4 inclusive) that should be read. The corresponding constants are provided: CHAR_VALUE = 1, SHORT_VALUE = 2 and INT_VALUE (LONG_VALUE) = 4, so the function can read the whole value of char, short, int or long type.

Return Value

A value of the int type. The result of this function must be explicitly cast to a target type, i.e. to the type of data that you need to read. Since a value of the int type is returned, it can be easily converted to any integer value. The file pointer is shifted by the number of bytes read.


When reading less than 4 bytes, the received result is always positive. If one or two bytes are read, the sign of the number can be determined by explicit casting to type char (1 byte) or short (2 bytes). Getting the sign for a three-byte number is not trivial, since there is no corresponding underlying type.

Example (the file obtained after executing the example for FileWriteInteger() function is used here)

//|                                         Demo_FileReadInteger.mq4 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                     |
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      ""
#property version   "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 1
//---- plot Label1
#property indicator_label1  "Trends"
#property indicator_type1   DRAW_SECTION
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  2
//--- parameters for data reading
input string InpFileName="Trend.bin"// File name
input string InpDirectoryName="Data"// Folder name
//--- global variables
int      ind=0;
int      size=0;
datetime time_buff[];
//--- indicator buffers
double   buff[];
//| Custom indicator initialization function                         |
int OnInit()
   int def_size=100;
//--- allocate memory for the array
//--- open the file
   int file_handle=FileOpen(InpDirectoryName+"//"+InpFileName,FILE_READ|FILE_BIN);
      PrintFormat("%s file is available for reading",InpFileName);
      PrintFormat("File path: %s\\Files\\",TerminalInfoString(TERMINAL_DATA_PATH));
      //--- additional variables
      int    arr_size;
      uchar  arr[];
      //--- read data from the file
         //--- find out how many bytes are used for writing the time
         for(int i=0;i<arr_size;i++)
         //--- store the time value
         //--- increase the sizes of the arrays if they are filled
      //--- close the file
      PrintFormat("Data is read, %s file is closed",InpFileName);
      PrintFormat("Failed to open %s file, Error code = %d",InpFileName,GetLastError());
//--- bind the array to the indicator buffer
//---- set the indicator values that will not be visible on the chart
//--- set indexing as timeseries
//| 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 bars that have not been handled yet
   for(int i=prev_calculated;i<rates_total;i++)
      //--- 0 by default
      for(int j=0;j<size;j++)
         //--- if dates coincide, set as close price
            //--- set as close price

See also

IntegerToString(), StringToInteger(), Integer types, FileWriteInteger()