MQL4参考 文件函数 FileReadLong


The function reads an integer of long type (8 bytes) from the current position of the binary file.

long  FileReadLong(
   int  file_handle    // File handle



[in]  File descriptor returned by FileOpen().

Return Value

The value of long type.

Example (the file obtained during the execution of an example for FileWriteLong() function is used here)

//|                                            Demo_FileReadLong.mq5 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                     |
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      ""
#property version   "1.00"
#property indicator_buffers 1
//---- plot Label1
#property indicator_label1  "Volume"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrYellow
#property indicator_style1  STYLE_SOLID
#property indicator_width1  2
#property indicator_separate_window
//--- parameters for data reading
input string InpFileName="Volume.bin"// file name
input string InpDirectoryName="Data";  // directory name
//--- global variables
int      ind=0;
int      size=0;
long     volume_buff[];
datetime time_buff[];
//--- indicator buffers
double   buff[];
//| Custom indicator initialization function                         |
int OnInit()
//--- open the file
   int file_handle=FileOpen(InpDirectoryName+"//"+InpFileName,FILE_READ|FILE_BIN);
      PrintFormat("%s file is open for writing",InpFileName);
      PrintFormat("File path: %s\\Files\\",TerminalInfoString(TERMINAL_DATA_PATH));
      //--- first, read the amount of data in the file
      //--- allocate memory for the arrays
      //--- read data from the file
      for(int i=0;i<size;i++)
      //--- 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 with 0 index
//---- set the indicator values that will be visible on the chart
//| 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
      //--- check if any data is still present
         for(int j=ind;j<size;j++)
            //--- if dates coincide, the value from the file is used
//--- return value of prev_calculated for next call

See also

Integer types, FileReadInteger(), FileWriteLong()