MQL4参考 文件函数 FileWriteDouble

FileWriteDouble

The function writes the value of a double parameter to a file, starting from the current position of the file pointer.

uint  FileWriteDouble(
   int     file_handle,     // File handle
   double  value            // Value to write
   );

Parameters

file_handle

[in]  File descriptor returned by FileOpen().

value

[in]   The value of double type.

Return Value

If successful the function returns the number of bytes written or 0 in case of error. If successful, the number of bytes written corresponds to the data type size (sizeof(double)=8). To obtain information about the error call the GetLastError() function. The file pointer is shifted by the same number of bytes.

Example:

//+------------------------------------------------------------------+
//|                                         Demo_FileWriteDouble.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
input int                InpMAPeriod=10;                   // Smoothing period
input int                InpMAShift=0;                     // Indicator shift
input ENUM_MA_METHOD     InpMAMethod=MODE_SMA;             // Smoothing type
input ENUM_APPLIED_PRICE InpAppliedPrice=PRICE_CLOSE;      // price type
//--- parameters for writing data to the file
input string             InpFileName="MA.bin";             // File name
input string             InpDirectoryName="Data";          // Folder name
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   datetime date_finish=TimeCurrent();
   double   ma_buff[];
   datetime time_buff[];
   int      size;
//--- set indexing as timeseries
   ArraySetAsSeries(ma_buff,true);
   ArraySetAsSeries(time_buff,true);
//--- reset last erro
   ResetLastError();
//--- copying the time from last 1000 bars
   int copied=CopyTime(NULL,0,0,1000,time_buff);
   if(copied<=0)
     {
      PrintFormat("Failed to copy time values. Error code = %d",GetLastError());
      return;
     }
//--- prepare ma_buff[] array
   ArrayResize(ma_buff,copied);
//--- copy the values of iMA indicator
   for(int i=0;i<copied;i++)
     {
      ma_buff[i]=iMA(InpSymbolName,InpSymbolPeriod,InpMAPeriod,InpMAShift,InpMAMethod,InpAppliedPrice,i);
     }
//---
   PrintFormat("The values starting from %s to %s will be written to file.",TimeToString(time_buff[copied-1]),TimeToString(time_buff[0]));
//--- get size
   size=ArraySize(ma_buff);
//--- open the file for writing the indicator 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));
      //--- first, write the size of data sample
      uint byteswritten=FileWriteDouble(file_handle,(double)size,DOUBLE_VALUE);
      //--- check the number of bytes written
      if(byteswritten!=sizeof(double))
        {
         PrintFormat("Error in FileWriteDouble. Error code=%d",GetLastError());
         //--- close the file
         FileClose(file_handle);
         return;
        }
      //--- write the indicator time and value to the file
      for(int i=0;i<size;i++)
        {
         byteswritten=FileWriteDouble(file_handle,(double)time_buff[i],DOUBLE_VALUE);
         //--- check the number of bytes written
         if(byteswritten!=sizeof(double))
           {
            PrintFormat("Error in FileWriteDouble. Error code=%d",GetLastError());
            //--- close the file
            FileClose(file_handle);
            return;
           }
         byteswritten=FileWriteDouble(file_handle,ma_buff[i],DOUBLE_VALUE);
         //--- check number of bytes written
         if(byteswritten!=sizeof(double))
           {
            PrintFormat("Error in FileWriteDouble. Error code=%d",GetLastError());
            //--- close the file
            FileClose(file_handle);
            return;
           }
        }
      //--- close the file
      FileClose(file_handle);
      PrintFormat("Data is written, %s file is closed",InpFileName);
     }
   else
      PrintFormat("Failed to open %s file, Error code = %d",InpFileName,GetLastError());
  }

See also

Real types (double, float)