MQL4参考 文件函数 FileReadDouble

FileReadDouble

Reads a double-precision floating point number (double) from the current position of the binary file.

double  FileReadDouble(
   int  file_handle,       // File handle
   int  size=DOUBLE_VALUE  // Size
   );

Parameters

file_handle

[in]  File descriptor returned by FileOpen().

size=DOUBLE_VALUE

Number of bytes (up to 8 inclusive), that should be read. The corresponding constants are provided: DOUBLE_VALUE = 8, FLOAT_VALUE = 4, so the function can read the whole value of double or float type.

Return Value

The value of double type.

Note

For more details about the error, call GetLastError().

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

//+------------------------------------------------------------------+
//|                                          Demo_FileReadDouble.mq4 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 1
//---- plot Label1
#property indicator_label1  "MA"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrGreen
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
#property indicator_separate_window
//--- data reading parameters
input string InpFileName="MA.bin";    // File name
input string InpDirectoryName="Data"// Folder name
//--- global variables
int      ind=0;
int      size=0;
double   ma_buff[];
datetime time_buff[];
//--- indicator buffer
double   buff[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- open the file
   ResetLastError();
   int file_handle=FileOpen(InpDirectoryName+"//"+InpFileName,FILE_READ|FILE_BIN);
   if(file_handle!=INVALID_HANDLE)
     {
      PrintFormat("%s file is available for reading",InpFileName);
      PrintFormat("File path: %s\\Files\\",TerminalInfoString(TERMINAL_DATA_PATH));
      //--- first, read the amount of data in the file
      size=(int)FileReadDouble(file_handle,DOUBLE_VALUE);
      //--- allocate memory for the arrays
      ArrayResize(ma_buff,size);
      ArrayResize(time_buff,size);
      //--- read data from the file
      for(int i=0;i<size;i++)
        {
         time_buff[i]=(datetime)FileReadDouble(file_handle,DOUBLE_VALUE);
         ma_buff[i]=FileReadDouble(file_handle,DOUBLE_VALUE);
        }
      //--- close the file
      FileClose(file_handle);
      PrintFormat("Data is written, %s file is closed",InpFileName);
     }
   else
     {
      PrintFormat("Failed to open %s file, Error code = %d",InpFileName,GetLastError());
      return(INIT_FAILED);
     }
//--- bind the array to the indicator buffer with index 0
   SetIndexBuffer(0,buff,INDICATOR_DATA);
//---- set the indicator values that will not be visible on the chart
   SetIndexEmptyValue(0,0.0);
//--- set indexing as timeseries
   ArraySetAsSeries(time_buff,true);
   ArraySetAsSeries(ma_buff,true);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| 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[])
  {
   ArraySetAsSeries(time,true);
//--- the loop for the bars that have not been handled yet
   for(int i=prev_calculated;i<rates_total;i++)
     {
      //--- 0 by default
      buff[i]=0;
      for(int j=0;j<size;j++)
        {
         //--- if the dates coincide, the value from the file is used
         if(time[i]==time_buff[j])
           {
            buff[i]=ma_buff[j];
           }
        }
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }

See also

Real types (double, float), StringToDouble(), DoubleToString(), FileWriteDouble()