FileWriteInteger

函数编辑整型参量值到二次文件中,从文件指标当前仓位开始。

uint  FileWriteInteger(

   int  file_handle,        // 文件句柄

   int  value,              // 被编写值

   int  size=INT_VALUE      // 字节大小

   );

参量

file_handle

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

value

[in] 整数值。

size=INT_VALUE

[in] 字节数量(最多包括4字节)可以编辑。提供类似常量:CHAR_VALUE=1, SHORT_VALUE=2 和 INT_VALUE=4,因此函数可以编辑图表型,双字符型,短型,长短型,整型或者长整型的整数值。

返回值

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

示例 :

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

//|                                        Demo_FileWriteInteger.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="Trend.bin"; // 文件名称

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

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

//| 脚本程序启动函数                                                   |

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

void OnStart()

  {

   datetime date_finish=TimeCurrent();

   double   close_buff[];

   datetime time_buff[];

   int      size;

//--- 重置错误值

   ResetLastError();

//--- 复制每个柱的收盘价

   if(CopyClose(InpSymbolName,InpSymbolPeriod,InpDateStart,date_finish,close_buff)==-1)

     {

      PrintFormat("Failed to copy the values of close prices. Error code = %d",GetLastError());

      return;

     }

//--- 复制每个柱形的时间

   if(CopyTime(InpSymbolName,InpSymbolPeriod,InpDateStart,date_finish,time_buff)==-1)

     {

      PrintFormat("Failed to copy time values. Error code = %d",GetLastError());

      return;

     }

//--- 接收缓冲区大小

   size=ArraySize(close_buff);

//--- 打开编写值的文件(如果文件不在,则自动创建)

   ResetLastError();

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

   if(file_handle!=INVALID_HANDLE)

     {

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

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

      //--- 

      int   up_down=0; // 趋势标识

      int   arr_size;  // arr 数组大小

      uchar arr[];     // uchar 类型数组

      //--- 写下文件的时间值

      for(int i=0;i<size-1;i++)

        {

         //--- 比较当前柱和下一柱的收盘价

         if(close_buff[i]<=close_buff[i+1])

           {

            if(up_down!=1)

              {

               //--- 使用FileWriteInteger写下文件的日期值

               StringToCharArray(TimeToString(time_buff[i]),arr);

               arr_size=ArraySize(arr);

               //--- 首先,写下数组中的交易品种数量

               FileWriteInteger(file_handle,arr_size,INT_VALUE);

               //--- 写下交易品种

               for(int j=0;j<arr_size;j++)

                  FileWriteInteger(file_handle,arr[j],CHAR_VALUE);

               //--- 改变趋势标识

               up_down=1;

              }

           }

         else

           {

            if(up_down!=-1)

              {

               //--- 使用FileWriteInteger写下文件的日期值

               StringToCharArray(TimeToString(time_buff[i]),arr);

               arr_size=ArraySize(arr);

               //--- 首先,写下数组中的交易品种数量

               FileWriteInteger(file_handle,arr_size,INT_VALUE);

               //--- 写下交易品种

               for(int j=0;j<arr_size;j++)

                  FileWriteInteger(file_handle,arr[j],CHAR_VALUE);

               //--- 改变趋势标识

               up_down=-1;

              }

           }

        }

      //--- 关闭文件

      FileClose(file_handle);

      PrintFormat("Data is written, %s file is closed",InpFileName);

     }

   else

      PrintFormat("Failed to open %s file, Error code = %d",InpFileName,GetLastError());

  }

另见

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