MQL4参考 文件函数 FileReadFloat

FileReadFloat

Reads the single-precision floating point number (float) from the current position of the binary file.

float  FileReadFloat(
   int  file_handle    // File handle
   );

Parameters

file_handle

[in]  File descriptor returned by FileOpen().

Return Value

The value of float type.

Note

For more details about the error, call GetLastError().

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

//+------------------------------------------------------------------+
//|                                           Demo_FileReadFloat.mq4 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//--- parameters for data reading
input string InpFileName="Close.bin"// file name
input string InpDirectoryName="Data"// directory name
//--- global variables
int      size=0;
double   close_buff[];
datetime time_buff[];
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- open the file
   ResetLastError();
   int file_handle=FileOpen(InpDirectoryName+"//"+InpFileName,FILE_READ|FILE_BIN);
   if(file_handle!=INVALID_HANDLE)
     {
      PrintFormat("%s file is available for reading",InpFileName);
      PrintFormat("File path: %s\\Files\\",TerminalInfoString(TERMINAL_DATA_PATH));
      //--- read data from the file
      while(!FileIsEnding(file_handle))
        {
         size++;
         //--- check size
         if(size>ArraySize(time_buff)) ArrayResize(time_buff,size,100);
         if(size>ArraySize(close_buff)) ArrayResize(close_buff,size,100);
         //--- read time and price values
         time_buff[size-1]=(datetime)FileReadDouble(file_handle);
         close_buff[size-1]=(double)FileReadFloat(file_handle);
        }
      //--- close the file
      FileClose(file_handle);
      PrintFormat("Data is read, %s file is closed",InpFileName);
      //--- check arrays
      if(ArraySize(time_buff)==ArraySize(close_buff))
        {
         //--- print data
         PrintFormat("Read data:%d",ArraySize(time_buff));
         for(int i=0; i<ArraySize(time_buff); i++) PrintFormat("%d, time=%s, close price=%s",i,TimeToString(time_buff[i]),DoubleToString(close_buff[i],_Digits));
        }
        else
        Print("Error in data.");
     }
   else
     {
      PrintFormat("Failed to open %s file, Error code = %d",InpFileName,GetLastError());
      return;
     }
  }

See also

Real types (double, float), FileReadDouble(), FileWriteFloat()