跳至内容

FileReadFloat

FileReadFloat

读取二进制文件当前位置的单精度浮点数(float)。

float  FileReadFloat(
   int  file_handle    // File handle
   );

参数

file_handle

[in] 由FileOpen()返回的文件描述符。

返回值

float类型的数值。

注意

关于错误的更多详细信息,请调用GetLastError()

示例(此处使用了执行FileWriteFloat()函数后获得的文件)

//+------------------------------------------------------------------+
//|                                           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;
     }
  }

参见

实数类型(double, float), FileReadDouble(), FileWriteFloat()

最后更新于