Download MetaTrader 5
MQL5 ReferenceFile FunctionsFileWriteStruct 


The function writes into a bin-file contents of a structure passed as a parameter, starting from the current position of the file pointer.

uint  FileWriteStruct(
   int          file_handle,       // File handle
   const void&  struct_object,     // link to an object
   int          size=-1            // size to be written in bytes



[in]  File descriptor returned by FileOpen().


[in] Reference to the object of this structure. The structure should not contain strings, dynamic arrays or virtual functions.


[in] Number of bytes that you want to record. If size is not specified or the specified number of bytes is greater than the size of the structure, the entire structure is written.

Return Value

If successful the function returns the number of bytes written. The file pointer is shifted by the same number of bytes.


//|                                          Demo_FileWiteStruct.mq5 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                     |
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      ""
#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="EURUSD.txt";         // file name
input string          InpDirectoryName="Data";          // directory name
//| Structure for storing candlestick data                           |
struct candlesticks
   double            open;  // open price
   double            close; // close price
   double            high;  // high price
   double            low;   // low price
   datetime          date;  // date
//| Script program start function                                    |
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[];
//--- reset the error value
//--- receive the time of the arrival of the bars from the range
      PrintFormat("Failed to copy time values. Error code = %d",GetLastError());
//--- receive high prices of the bars from the range
      PrintFormat("Failed to copy values of high prices. Error code = %d",GetLastError());
//--- receive low prices of the bars from the range
      PrintFormat("Failed to copy values of low prices. Error code = %d",GetLastError());
//--- receive open prices of the bars from the range
      PrintFormat("Failed to copy values of open prices. Error code = %d",GetLastError());
//--- receive close prices of the bars from the range
      PrintFormat("Failed to copy values of close prices. Error code = %d",GetLastError());
//--- define dimension of the arrays
//--- save all data in the structure array
   for(int i=0;i<size;i++)
//--- open the file for writing the structure array to the file (if the file is absent, it will be created automatically)
   int file_handle=FileOpen(InpDirectoryName+"//"+InpFileName,FILE_READ|FILE_WRITE|FILE_BIN|FILE_COMMON);
      PrintFormat("%s file is open for writing",InpFileName);
      PrintFormat("File path: %s\\Files\\",TerminalInfoString(TERMINAL_COMMONDATA_PATH));
      //--- prepare the counter of the number of bytes
      uint counter=0;
      //--- write array values in the loop
      for(int i=0;i<size;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");
      //--- close the file
      PrintFormat("Data is written, %s file is closed",InpFileName);
      PrintFormat("Failed to open %s file, Error code = %d",InpFileName,GetLastError());

See also

Structures and classes

Updated: 2015.12.03