MQL4参考 文件函数 FileReadString


The function reads a string from the current position of a file pointer in a file.

string  FileReadString(
   int  file_handle,     // File handle
   int  length=0         // Length of the string



[in]  File descriptor returned by FileOpen().


[in]  Number of characters to read.

Return Value

Line read (string).


When reading from a bin-file. the length of a string to read must be specified. When reading from a txt-file the string length is not required, and the string will be read from the current position to the line feed character "\r\n". When reading from a csv-file, the string length isn't required also, the string will be read from the current position till the nearest delimiter or till the text string end character.

If the file is opened with FILE_ANSI flag, then the line read is converted to Unicode.

Example (the file obtained after executing the example for FileWriteInteger() function is used here)

//--- display the window of input parameters when launching the script
#property script_show_inputs
//--- parameters for data reading
input string InpFileName="Trend.bin"// file name
input string InpDirectoryName="Data"// directory name
//| Script program start function                                    |
void OnStart()
//--- open the file
   int file_handle=FileOpen(InpDirectoryName+"//"+InpFileName,FILE_READ|FILE_BIN|FILE_ANSI);
      PrintFormat("%s file is available for reading",InpFileName);
      PrintFormat("File path: %s\\Files\\",TerminalInfoString(TERMINAL_DATA_PATH));
      //--- additional variables
      int    str_size;
      string str;
      //--- read data from the file
         //--- find out how many symbols are used for writing the time
         //--- read the string
         //--- print the string
      //--- close the file
      PrintFormat("Data is read, %s file is closed",InpFileName);
      PrintFormat("Failed to open %s file, Error code = %d",InpFileName,GetLastError());

See also

String Type, Conversion Functions, FileWriteInteger()