FileWriteStruct

通过传递参量函数编辑二次结构文件目录,从文件指标的当前仓位开始。

uint  FileWriteStruct(
   int          file_handle,       // 文件句柄
   const void&  struct_object,     // 连接一个物件
   int          size=-1            // 写入字节大小
   );

参量

file_handle

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

struct_object

[in] 参考结构对象,结构不应该包含字符串, 动态数组 或者 虚拟函数

size=-1

[in] 想要记录的自己数量,如果大小不够指定或者指定字节数量大于结构大小,编辑全部结构。

返回值

如果成功,函数返回编辑的自己数量,文件指标以相同数量字节转换。

示例 :

//+------------------------------------------------------------------+
//|                                          Demo_FileWiteStruct.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 script_show_inputs
//--- 接收程序端数据的参数
input string          InpSymbolName="EURUSD";           // 货币组
input ENUM_TIMEFRAMES InpSymbolPeriod=PERIOD_H1;        // 时间帧
input datetime        InpDateStart=D'2013.01.01 00:00'; // 复制起始日期的数据
//--- 编写文件数据的参数
input string          InpFileName="EURUSD.txt";         // 文件名称
input string          InpDirectoryName="Data";          // 目录名称
//+------------------------------------------------------------------+
//| 存储蜡烛图数据的结构                                                |
//+------------------------------------------------------------------+
struct candlesticks
  {
   double            open;  // 开盘价
   double            close; // 收盘价
   double            high;  // 最高价
   double            low;   // 最低价
   datetime          date;  // 日期
  };
//+------------------------------------------------------------------+
//| 脚本程序启动函数                                                   |
//+------------------------------------------------------------------+
void OnStart()
  {
   datetime     date_finish=TimeCurrent();
   int          size;
   datetime     time_buff[];
   double       open_buff[];
   double       close_buff[];
   double       high_buff[];
   double       low_buff[];
   candlesticks cand_buff[];
//--- 重置错误值
   ResetLastError();
//--- 接收范围内柱形到达时间
   if(CopyTime(InpSymbolName,InpSymbolPeriod,InpDateStart,date_finish,time_buff)==-1)
     {
      PrintFormat("Failed to copy time values. Error code = %d",GetLastError());
      return;
     }
//--- 接收范围内柱形的最高价
   if(CopyHigh(InpSymbolName,InpSymbolPeriod,InpDateStart,date_finish,high_buff)==-1)
     {
      PrintFormat("Failed to copy values of high prices. Error code = %d",GetLastError());
      return;
     }
//--- 接收范围内柱形的最低价
   if(CopyLow(InpSymbolName,InpSymbolPeriod,InpDateStart,date_finish,low_buff)==-1)
     {
      PrintFormat("Failed to copy values of low prices. Error code = %d",GetLastError());
      return;
     }
//--- 接收范围内柱形的开盘价
   if(CopyOpen(InpSymbolName,InpSymbolPeriod,InpDateStart,date_finish,open_buff)==-1)
     {
      PrintFormat("Failed to copy values of open prices. Error code = %d",GetLastError());
      return;
     }
//--- 接收范围内柱形的收盘价
   if(CopyClose(InpSymbolName,InpSymbolPeriod,InpDateStart,date_finish,close_buff)==-1)
     {
      PrintFormat("Failed to copy values of close prices. Error code = %d",GetLastError());
      return;
     }
//--- 定义数组维数
   size=ArraySize(time_buff);
//--- 在结构数组保存所有数据
   ArrayResize(cand_buff,size);
   for(int i=0;i<size;i++)
     {
      cand_buff[i].open=open_buff[i];
      cand_buff[i].close=close_buff[i];
      cand_buff[i].high=high_buff[i];
      cand_buff[i].low=low_buff[i];
      cand_buff[i].date=time_buff[i];
     }
 
//--- 打开用于编写文件结构数组的文件(如果文件不在,则自动创建)
   ResetLastError();
   int file_handle=FileOpen(InpDirectoryName+"//"+InpFileName,FILE_READ|FILE_WRITE|FILE_BIN|FILE_COMMON);
   if(file_handle!=INVALID_HANDLE)
     {
      PrintFormat("%s file is open for writing",InpFileName);
      PrintFormat("File path: %s\\Files\\",TerminalInfoString(TERMINAL_COMMONDATA_PATH));
      //--- 准备字节数量的计数器
      uint counter=0;
      //--- 写下循环数组值
      for(int i=0;i<size;i++)
         counter+=FileWriteStruct(file_handle,cand_buff[i]);
      PrintFormat("%d bytes of information is written to %s file",InpFileName,counter);
      PrintFormat("Total number of bytes: %d * %d * %d = %d, %s",size,5,8,size*5*8,size*5*8==counter ? "Correct" : "Error");
      //--- 关闭文件
      FileClose(file_handle);
      PrintFormat("Data is written, %s file is closed",InpFileName);
     }
   else
      PrintFormat("Failed to open %s file, Error code = %d",InpFileName,GetLastError());
  }

另见

结构和类