FileReadLong

从二进制文件当前位置函数读取长型整数(8字节)。

long  FileReadLong(
   int  file_handle    // 文件句柄
   );

参量

file_handle

[in] 通过FileOpen()返回文件说明符。

返回值

长整型值。

示例 (执行FileWriteLong函数示例后获得的文件在这里使用)

//+------------------------------------------------------------------+
//|                                            Demo_FileReadLong.mq5 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_buffers 1
#property indicator_plots   1
//---- 图 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
//--- 读取数据的参数
input string InpFileName="Volume.bin"// 文件名称
input string InpDirectoryName="Data";  // 目录名称
//--- 全局变量
int      ind=0;
int      size=0;
long     volume_buff[];
datetime time_buff[];
//--- 指标缓冲区
double   buff[];
//+------------------------------------------------------------------+
//| 自定义指标初始化函数                                                |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- 打开文件
   ResetLastError();
   int file_handle=FileOpen(InpDirectoryName+"//"+InpFileName,FILE_READ|FILE_BIN);
   if(file_handle!=INVALID_HANDLE)
     {
      PrintFormat("%s file is open for writing",InpFileName);
      PrintFormat("File path: %s\\Files\\",TerminalInfoString(TERMINAL_DATA_PATH));
      //--- 首先,阅读文件中的数据量
      size=(int)FileReadLong(file_handle);
      //--- 为数组分配内存
      ArrayResize(volume_buff,size);
      ArrayResize(time_buff,size);
      //--- 读取文件数据
      for(int i=0;i<size;i++)
        {
         time_buff[i]=(datetime)FileReadLong(file_handle);
         volume_buff[i]=FileReadLong(file_handle);
        }
      //--- 关闭文件
      FileClose(file_handle);
      PrintFormat("Data is read, %s file is closed",InpFileName);
     }
   else
     {
      PrintFormat("Failed to open %s file, Error code = %d",InpFileName,GetLastError());
      return(INIT_FAILED);
     }
//--- 绑定数组和指数为0的指标缓冲区
   SetIndexBuffer(0,buff,INDICATOR_DATA);
//---- 设置图表上看不到的指标值
   PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| 自定义指标迭代函数                                                 |
//+------------------------------------------------------------------+
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,false);
//--- 还未处理的柱的循环
   for(int i=prev_calculated;i<rates_total;i++)
     {
      //--- 默认为0
      buff[i]=0;
      //--- 检查任何日期是否仍然存在
      if(ind<size)
        {
         for(int j=ind;j<size;j++)
           {
            //--- 如果日期一致,使用文件的值
            if(time[i]==time_buff[j])
              {
               buff[i]=(double)volume_buff[j];
               ind=j+1;
               break;
              }
           }
        }
     }
//--- 返回prev_calculated值,以便下次调用
   return(rates_total);
  }

另见

整数类型, FileReadInteger, FileWriteLong