FileRead

Reading data from a file of any type.

uint FileRead(
 int file_handle, // File handle
 ... // List of parameters to read
 );

Parameters

file_handle

[in] File descriptor returned by FileOpen().

...

[in] A comma-separated list of parameters to read from the file. Each parameter is passed by reference. The number of parameters must not exceed 256. Variables must be of basic types; arrays, structures, matrices and vectors are not allowed.

Return Value

Number of variables read. If fewer variables have been read than expected, the read error can be analyzed using GetLastError(). If the end of a line is encountered while reading from a CSV file, reading is interrupted, but no error is raised.

Note

FileRead mirrors the FileWrite function. The function is universal and reads from files of any type: CSV, TXT or BIN.

FileRead cannot read data of the string type from a binary file, because the length of the string to be read is unknown in advance.

When reading from a binary file, the number of bytes corresponding to the data type is read into each variable (char - 1, int - 4, long - 8, double - 8, etc).

When reading from a text file, the entire line is read into a variable of the string type up to its end, without the terminating end-of-line characters.

When reading from a CSV file, strings containing the text representation of the corresponding type are first read, and then the string is converted to a variable of the corresponding type (for examples, see FileReadNumber, FileReadBool, FileReadDateTime).

Example:

//+------------------------------------------------------------------+
//|                                                 TestFileRead.mq5 |
//|                                  Copyright 2026, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2026, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   uint     written,read;
//--- 13 variables of different types
   bool     bool_value=true;
   char     char_value=123;
   short    short_value=12345;  
   int      int_value=1234567;
   long     long_value=5000000000;
   float    float_value=FLT_EPSILON;
   double   double_value=M_PI;
   color    color1_value=clrAquamarine;
   color    color2_value=C'111,0,222';
   datetime datetime_value=TimeCurrent();
   complex  complex_value=1.1+0.1i;
   complexf complexf_value=5.5+0.5i;
   string   string_value="the quick brown fox jumps over the lazy dog";
//--- open a CSV file for writing
   int file_handle=FileOpen("test.csv",FILE_CSV|FILE_WRITE|FILE_ANSI);
//--- write 13 variables of different types
   written=FileWrite(file_handle,bool_value,char_value,short_value,int_value,long_value,float_value,double_value,color1_value,color2_value,datetime_value,string_value,complex_value,complexf_value);
   bool_value=false;
   char_value=124;
   short_value=12347;  
   int_value=1234569;
   long_value=5001000000;
//--- write 13 variables to the second line of the CSV file
   written+=FileWrite(file_handle,bool_value,char_value,short_value,int_value,long_value,float_value,double_value,color1_value,color2_value,datetime_value,string_value,complex_value,complexf_value);
   bool_value=false;
   char_value=125;
   short_value=12349;  
   int_value=1234571;
   long_value=5002000000;
//--- write 13 variables to the third line of the CSV file
   written+=FileWrite(file_handle,bool_value,char_value,short_value,int_value,long_value,float_value,double_value,color1_value,color2_value,datetime_value,string_value,complex_value,complexf_value);
   FileClose(file_handle);
   Print(written," bytes written");
   bool     bool_value2;
   char     char_value2;
   short    short_value2;  
   int      int_value2;
   long     long_value2;
   float    float_value2;
   double   double_value2;
   color    color1_value2;
   color    color2_value2;
   datetime datetime_value2;
   complex  complex_value2;
   complexf complexf_value2;
   string   string_value2;
   string   string2_value="untouched string";
//--- open the written CSV file for reading
   file_handle=FileOpen("test.csv",FILE_CSV|FILE_READ|FILE_ANSI);
//--- try to read 1 more variable than there is in the line
   read=FileRead(file_handle,bool_value2,char_value2,short_value2,int_value2,long_value2,float_value2,double_value2,color1_value2,color2_value2,datetime_value2,string_value2,complex_value2,complexf_value2,string2_value);
   FileClose(file_handle);
   Print("read ",read," variables");
   Print(bool_value2," ",char_value2," ",short_value2," ",int_value2," ",long_value2," ",float_value2," ",double_value2," ",color1_value2," ",color2_value2," ",datetime_value2," ",string_value2," ",complex_value2," ",complexf_value2," ",string2_value);
   string txt_line1,txt_line2,txt_line3;
//--- open the written file as a text file
   file_handle=FileOpen("test.csv",FILE_TXT|FILE_READ|FILE_ANSI);
//--- read 3 lines from the text file
   read=FileRead(file_handle,txt_line1,txt_line2,txt_line3);
   Print("\nread ",read," lines");
   FileClose(file_handle);
//--- visually check that these are the same lines that were written
   Print(txt_line1);
   Print(txt_line2);
   Print(txt_line3);
//--- open a binary file for writing
   file_handle=FileOpen("test.bin",FILE_BIN|FILE_WRITE);
//--- write 12 variables of different types, except string
   written=FileWrite(file_handle,bool_value,char_value,short_value,int_value,long_value,float_value,double_value,color1_value,color2_value,datetime_value,complex_value,complexf_value);
   FileClose(file_handle);
   Print("");
   Print(written," bytes written");
   bool     bool_value3;
   char     char_value3;
   short    short_value3;  
   int      int_value3;
   long     long_value3;
   float    float_value3;
   double   double_value3;
   color    color1_value3;
   color    color3_value3;
   datetime datetime_value3;
   complex  complex_value3;
   complexf complexf_value3;
//--- open the written binary file for reading
   file_handle=FileOpen("test.bin",FILE_BIN|FILE_READ);
//--- read 12 variables of the same types
   read=FileRead(file_handle,bool_value3,char_value3,short_value3,int_value3,long_value3,float_value3,double_value3,color1_value3,color3_value3,datetime_value3,complex_value3,complexf_value3);
   FileClose(file_handle);
   Print("read ",read," variables");
//--- visually check that the data has been read correctly
   Print(bool_value3," ",char_value3," ",short_value3," ",int_value3," ",long_value3," ",float_value3," ",double_value3," ",color1_value3," ",color3_value3," ",datetime_value3," ",complex_value3," ",complexf_value3);
  }

Output in the Experts journal

554 bytes written
read 13 variables
true 123 12345 1234567 5000000000 1.1920928955078125e-07 3.141592653589793 clrAquamarine 111,0,222 2026.09.25 14:44:25 the quick brown fox jumps over the lazy dog (1.1,0.1) (5.5,0.5) untouched string
 
read 3 lines
true 123 12345 1234567 5000000000 1.1920928955078125e-07 3.141592653589793 clrAquamarine 111,0,222 2026.09.25 14:44:25 the quick brown fox jumps over the lazy dog (1.1,0.1) (5.5,0.5)
false 124 12347 1234569 5001000000 1.1920928955078125e-07 3.141592653589793 clrAquamarine 111,0,222 2026.09.25 14:44:25 the quick brown fox jumps over the lazy dog (1.1,0.1) (5.5,0.5)
false 125 12349 1234571 5002000000 1.1920928955078125e-07 3.141592653589793 clrAquamarine 111,0,222 2026.09.25 14:44:25 the quick brown fox jumps over the lazy dog (1.1,0.1) (5.5,0.5)
 
68 bytes written
read 12 variables
false 125 12349 1234571 5002000000 1.1920928955078125e-07 3.141592653589793 clrAquamarine 111,0,222 2026.09.25 14:44:25 (1.1,0.1) (5.5,0.5)

See Also

FileWrite, FileReadNumber, FileReadBool, FileReadDateTime