Reads all data of a specified binary file into a passed array of numeric types or simple structures. The function allows you to quickly read data of a known type into the appropriate array.

long  FileLoad(
   const string  file_name,         // File name
   void&         buffer[],          // An array of numeric types or simple structures
   int           common_flag=0      // A file flag, is searched in <data_folder>\MQL5\Files\ by default



[in]  The name of the file from which data will be read.


[out]  An array of numeric types or simple structures.


[in] A file flag indicating the operation mode. If the parameter is not specified, the file is searched in the subfolder MQL5\Files (or in <testing_agent_directory>\MQL5\Files in case of testing).

Return Value

The number of elements read or -1 in case of an error.


The FileLoad() function reads from a file the number of bytes multiple of the array element size. Suppose the file size is 10 bytes, and the function reads data into an array of type double (sizeof(double)=8). In this case the function will read only 8 bytes, the remaining 2 bytes at the end of the file will be dropped, and the function FileLoad() will return 1 (1 element read).


//|                                                Demo_FileLoad.mq5 |
//|                        Copyright 2016, MetaQuotes Software Corp. |
//|                                    |
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link      ""
#property version   "1.00"
#property copyright "Copyright 2016, MetaQuotes Software Corp."
#property link      ""
#property version   "1.00"
#property script_show_inputs
//--- input parameters
input int      bars_to_save=10; // Number of bars
//| Script program start function                                    |
void OnStart()
   string  filename=_Symbol+"_rates.bin";
   MqlRates rates[];
   int copied=CopyRates(_Symbol,_Period,0,bars_to_save,rates);
      PrintFormat(" CopyRates(%s) copied %d bars",_Symbol,copied);
      //---  Writing quotes to a file
         PrintFormat("FileSave() failed, error=%d",GetLastError());
      PrintFormat("Failed CopyRates(%s), error=",_Symbol,GetLastError());
//--- Now reading these quotes back to the file
   long count=FileLoad(filename,rates,FILE_COMMON);
      Print("Time\tOpen\tHigh\tLow\tClose\tTick Voulme\tSpread\tReal Volume");
      for(int i=0;i<count;i++)

See also

Structures and Classes, FileReadArray, FileReadStruct, FileSave