FileReadStruct

从二进制文件中函数读取以参量传递的结构,从文件指针的当前仓位开始。

uint  FileReadStruct(

   int          file_handle,        // 文件句柄

   const void&  struct_object,      // 读取的目标结构

   int          size=-1             // 字节中结构大小

   );

参量

file_handle

[in] 打开二进制文件的文件说明符。

struct_object

[out]  结构对象,结构不包括字符串, 动态数组 或者 虚拟函数

size=-1

[in]  可以读取字节数量,如果大小未标明或者指示值比结构大小要大,需要使用指定结构的精确大小。

返回值

如果成功,函数返回读取的字节数量,文件指标通过相同的字节数量移动。

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

//+------------------------------------------------------------------+

//|                                          Demo_FileReadStruct.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_separate_window

#property indicator_buffers 4

#property indicator_plots   1

//---- 图 Label1

#property indicator_label1  "Candles"

#property indicator_type1   DRAW_CANDLES

#property indicator_color1  clrOrange

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

#property indicator_separate_window

//--- 接收数据的参数

input string  InpFileName="EURUSD.txt"; // 文件名称

input string  InpDirectoryName="Data";  // 目录名称

//+------------------------------------------------------------------+

//| 存储蜡烛图数据的结构                                                |

//+------------------------------------------------------------------+

struct candlesticks

  {

   double            open;  // 开盘价

   double            close; // 收盘价

   double            high;  // 最高价

   double            low;   // 最低价

   datetime          date;  // 日期

  };

//--- 指标缓冲区

double       open_buff[];

double       close_buff[];

double       high_buff[];

double       low_buff[];

//--- 全局变量

candlesticks cand_buff[];

int          size=0;

int          ind=0;

//+------------------------------------------------------------------+

//| 自定义指标初始化函数                                                |

//+------------------------------------------------------------------+

int OnInit()

  {

   int default_size=100;

   ArrayResize(cand_buff,default_size);

//--- 打开文件

   ResetLastError();

   int file_handle=FileOpen(InpDirectoryName+"//"+InpFileName,FILE_READ|FILE_BIN|FILE_COMMON);

   if(file_handle!=INVALID_HANDLE)

     {

      PrintFormat("%s file is available for reading",InpFileName);

      PrintFormat("File path: %s\\Files\\",TerminalInfoString(TERMINAL_COMMONDATA_PATH));

      //--- 读取文件数据

      while(!FileIsEnding(file_handle))

        {

         //--- 编写数组数据

         FileReadStruct(file_handle,cand_buff[size]);

         size++;

         //--- 检查数组是否超过

         if(size==default_size)

           {

            //--- 增加数组维数

            default_size+=100;

            ArrayResize(cand_buff,default_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,open_buff,INDICATOR_DATA);

   SetIndexBuffer(1,high_buff,INDICATOR_DATA);

   SetIndexBuffer(2,low_buff,INDICATOR_DATA);

   SetIndexBuffer(3,close_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

      open_buff[i]=0;

      close_buff[i]=0;

      high_buff[i]=0;

      low_buff[i]=0;

      //--- 检查任何日期是否仍然存在

      if(ind<size)

        {

         for(int j=ind;j<size;j++)

           {

            //--- 如果日期一致,使用文件的值

            if(time[i]==cand_buff[j].date)

              {

               open_buff[i]=cand_buff[j].open;

               close_buff[i]=cand_buff[j].close;

               high_buff[i]=cand_buff[j].high;

               low_buff[i]=cand_buff[j].low;

               //--- 增加计数器

               ind=j+1;

               break;

              }

           }

        }

     }

//--- 返回prev_calculated 值,以便下次调用

   return(rates_total);

  }

另见

结构和类, FileWriteStruct