FileReadInteger

从当前文件指示位置,函数读取整型,短型或图表型值,取决于规定字节长度。

int  FileReadInteger(
   int  file_handle,        // 文件句柄
   int  size=INT_VALUE      // 字节中整数的大小
   );

参量

file_handle

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

size=INT_VALUE

[in]  应该阅读的字节数(最多到4)。提供对应的常量:CHAR_VALUE = 1, SHORT_VALUE = 2 和 INT_VALUE = 4,所以函数可以阅读字符型,短整型或整型的整个值。

返回值

整型的值。该函数的值必须明确计算目标类型,例如您需要阅读的数据类型。因为返回整型的值,它可以轻松地转到任何整型值。文件指针就是阅读字节数的移动装置。

注释

当阅读少于4字节时,接收的结果始终是正数。如果阅读一个或两个字节,数字符号可以通过明确计算字符型(1个字节)或短整型(2个字节)来确定。获得3字节数字的符号并不重要,因为没有相应的基础类型

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

//+------------------------------------------------------------------+
//|                                         Demo_FileReadInteger.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_chart_window
#property indicator_buffers 1
#property indicator_plots   1
//---- 图 Label1
#property indicator_label1  "Trends"
#property indicator_type1   DRAW_SECTION
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- 读取数据的参数
input string InpFileName="Trend.bin"// 文件名称
input string InpDirectoryName="Data"// 目录名称
//--- 全局变量
int      ind=0;
int      size=0;
datetime time_buff[];
//--- 指标缓冲区
double   buff[];
//+------------------------------------------------------------------+
//| 自定义指标初始化函数                                                |
//+------------------------------------------------------------------+
int OnInit()
  {
   int def_size=100;
//--- 为数组分配内存
   ArrayResize(time_buff,def_size);
//--- 打开文件
   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));
      //--- 附加变量
      int    arr_size;
      uchar  arr[];
      //--- 读取文件数据
      while(!FileIsEnding(file_handle))
        {
         //--- 找出多少交易品种用于编写时间
         arr_size=FileReadInteger(file_handle,INT_VALUE);
         ArrayResize(arr,arr_size);
         for(int i=0;i<arr_size;i++)
            arr[i]=(char)FileReadInteger(file_handle,CHAR_VALUE);
         //--- 存储时间价值
         time_buff[size]=StringToTime(CharArrayToString(arr));
         size++;
         //--- 如果超过增加数组大小
         if(size==def_size)
           {
            def_size+=100;
            ArrayResize(time_buff,def_size);
           }
        }
      //--- 关闭文件
      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);
     }
//--- 绑定数组和指标缓冲区
   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);
   ArraySetAsSeries(close,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]=close[i];
               //--- 增加计数器
               ind=j+1;
               break;
              }
           }
        }
     }
//--- 返回prev_calculated 值,以便下次调用
   return(rates_total);
  }

另见

整数到字符串, 字符串到整数, 整数类型, FileWriteInteger