跳至内容

FileWriteInteger

FileWriteInteger

该函数将 int 参数的值写入到二进制文件中,从文件指针的当前位置开始。

uint  FileWriteInteger(
   int  file_handle,        // File handle
   int  value,              // Value to be written
   int  size=INT_VALUE      // Size in bytes
   );

参数

file_handle

[in] 由 FileOpen() 返回的文件描述符。

value

[in] 整数值。

size=INT_VALUE

[in] 要写入的字节数(最多包含 4 个),对应的常量如下:CHAR_VALUE=1,SHORT_VALUE=2 和 INT_VALUE=4,因此该函数可以写入 char、uchar、short、ushort、int 或 uint 类型的整数值。

返回值

如果成功,函数返回写入的字节数;如果出错,则返回 0。如果成功,写入的字节数对应于数据类型的大小。要获取关于错误的信息,请调用 GetLastError() 函数。文件指针也会相应移动相同的字节数。

示例:

//+------------------------------------------------------------------+
//|                                        Demo_FileWriteInteger.mq4 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//--- show the window of input parameters when launching the script
#property script_show_inputs
//--- parameters for receiving data from the terminal
input string             InpSymbolName="EURUSD";     // Currency pair
input ENUM_TIMEFRAMES    InpSymbolPeriod=PERIOD_H1;  // Time frame
//--- parameters for writing data to the file
input string             InpFileName="Trend.bin";    // File name
input string             InpDirectoryName="Data";    // Folder name
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   double   close_buff[];
   datetime time_buff[];
   int      size;
//--- set indexing as timeseries
   ArraySetAsSeries(close_buff,true);
   ArraySetAsSeries(time_buff,true);
//--- reset the error value
   ResetLastError();
//--- copy the close price for each bar
   if(CopyClose(InpSymbolName,InpSymbolPeriod,0,1000,close_buff)==-1)
     {
      PrintFormat("Failed to copy the values of close prices. Error code = %d",GetLastError());
      return;
     }
//--- copy the time for each bar
   if(CopyTime(InpSymbolName,InpSymbolPeriod,0,1000,time_buff)==-1)
     {
      PrintFormat("Failed to copy time values. Error code = %d",GetLastError());
      return;
     }
//--- get the buffer size
   size=ArraySize(close_buff);
//--- open the file for writing the values (if the file is absent, it will be created automatically)
   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));
      //---
      uint  byteswritten;// number of bytes written
      int   up_down=0;   // trend flag
      int   arr_size;    // arr array size
      uchar arr[];       // uchar type array
      //--- write time values to the file
      for(int i=0;i<size-1;i++)
        {
         //--- compare close prices of the current and next bars
         if(close_buff[i]<=close_buff[i+1])
           {
            if(up_down!=1)
              {
               //--- write date value to the file using FileWriteInteger
               StringToCharArray(TimeToString(time_buff[i]),arr);
               arr_size=ArraySize(arr);
               //--- first, write the number of chars in the array
               byteswritten=FileWriteInteger(file_handle,arr_size,INT_VALUE);
               //--- checking the number of bytes written
               if(byteswritten!=sizeof(int))
                 {
                  PrintFormat("Error in FileWriteInteger. Error code=%d",GetLastError());
                  //--- close the file
                  FileClose(file_handle);
                  return;
                 }
               //--- write the chars
               for(int j=0;j<arr_size;j++)
                 {
                  byteswritten=FileWriteInteger(file_handle,arr[j],CHAR_VALUE);
                  //--- checking the number of bytes written
                  if(byteswritten!=sizeof(char))
                    {
                     PrintFormat("Error in FileWriteInteger. Error code=%d",GetLastError());
                     //--- close the file
                     FileClose(file_handle);
                     return;
                    }
                 }
               //--- change the trend flag
               up_down=1;
              }
           }
         else
           {
            if(up_down!=-1)
              {
               //--- write the date value to the file using FileWriteInteger
               StringToCharArray(TimeToString(time_buff[i]),arr);
               arr_size=ArraySize(arr);
               //--- first, write the number of chars in the array
               byteswritten=FileWriteInteger(file_handle,arr_size,INT_VALUE);
               //--- checking the number of bytes written
               if(byteswritten!=sizeof(int))
                 {
                  PrintFormat("Error in FileWriteInteger. Error code=%d",GetLastError());
                  //--- close the file
                  FileClose(file_handle);
                  return;
                 }
               //--- write chars
               for(int j=0;j<arr_size;j++)
                 {
                  byteswritten=FileWriteInteger(file_handle,arr[j],CHAR_VALUE);
                  //--- checking the number of bytes written
                  if(byteswritten!=sizeof(char))
                    {
                     PrintFormat("Error in FileWriteInteger. Error code=%d",GetLastError());
                     //--- close the file
                     FileClose(file_handle);
                     return;
                    }
                 }
               //--- change the trend flag
               up_down=-1;
              }
           }
        }
      //--- close the file
      FileClose(file_handle);
      PrintFormat("Data is written, %s file is closed",InpFileName);
     }
   else
      PrintFormat("Data is written, %s file is closed",InpFileName,GetLastError());
  }

另请参阅

IntegerToString()StringToInteger()整数类型

最后更新于