MQL4参考 文件函数 FileWriteLong

FileWriteLong

The function writes the value of the long-type parameter to a bin-file, starting from the current position of the file pointer.

uint  FileWriteLong(
   int   file_handle,     // File handle
   long  value            // Value to be written
   );

Parameters

file_handle

[in]  File descriptor returned by FileOpen().

value

[in] Value of type long.

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(long)=8). To obtain information about the error call the GetLastError() function. The file pointer is shifted by the same number of bytes.

Example:

//+------------------------------------------------------------------+
//|                                           Demo_FileWriteLong.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"
//--- 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 datetime        InpDateStart=D'2013.01.01 00:00'; // data copying start date
//--- parameters for writing data to the file
input string          InpFileName="Volume.bin"// file name
input string          InpDirectoryName="Data";  // directory name
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   datetime date_finish=TimeCurrent();
   long     volume_buff[];
   datetime time_buff[];
   int      size;
//--- reset the error value
   ResetLastError();
//--- copy tick volumes for each bar
   if(CopyTickVolume(InpSymbolName,InpSymbolPeriod,InpDateStart,date_finish,volume_buff)==-1)
     {
      PrintFormat("Failed to copy values of the tick volume. Error code = %d",GetLastError());
      return;
     }
//--- copy the time for each bar
   if(CopyTime(InpSymbolName,InpSymbolPeriod,InpDateStart,date_finish,time_buff)==-1)
     {
      PrintFormat("Failed to copy time values. Error code = %d",GetLastError());
      return;
     }
//--- receive the buffer size
   size=ArraySize(volume_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 data sample size
      uint byteswritten=FileWriteLong(file_handle,(long)size);
      //--- check the number of bytes written
      if(byteswritten!=sizeof(long))
        {
         PrintFormat("Error in FileWriteLong. Error code=%d",GetLastError());
         //--- close the file
         FileClose(file_handle);
         return;
        }
      //--- write time and volume values to file
      for(int i=0;i<size;i++)
        {
         byteswritten=FileWriteLong(file_handle,(long)time_buff[i]);
         //--- check the number of bytes written
         if(byteswritten!=sizeof(long))
           {
            PrintFormat("Error in FileWriteLong. Error code=%d",GetLastError());
            //--- close the file
            FileClose(file_handle);
            return;
           }
         byteswritten=FileWriteLong(file_handle,volume_buff[i]);
         //--- check the number of bytes written
         if(byteswritten!=sizeof(long))
           {
            PrintFormat("Error in FileWriteLong. 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

Integer types, FileWriteInteger()