跳至内容

FileGetInteger

FileGetInteger

获取文件的整数属性。该函数有两种实现方式。

  1. 通过文件句柄获取属性。
long  FileGetInteger(
   int                         file_handle,   // File handle
   ENUM_FILE_PROPERTY_INTEGER  property_id    // Property ID
   );
  1. 通过文件名获取属性。
long  FileGetInteger(
   const string                file_name,            // File name
   ENUM_FILE_PROPERTY_INTEGER  property_id,          // Property ID
   bool                        common_folder=false   // The file is viewed in a local folder (false)
   );                                                // or a common folder of all terminals (true)

参数

file_handle

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

file_name

[in] 文件名。

property_id

[in] 文件属性ID。该值可以是 ENUM_FILE_PROPERTY_INTEGER 枚举中的某个值。如果使用函数的第二种实现方式,只能获取 以下属性的值:FILE_EXISTS、FILE_CREATE_DATE、FILE_MODIFY_DATE、FILE_ACCESS_DATE 和 FILE_SIZE。

common_folder=false

[in] 指向文件位置。如果参数为 false,则查看终端数据文件夹。否则,假设文件位于所有终端的共享文件夹 \Terminal\Common\Files (FILE_COMMON)。

返回值

属性的值。发生错误时,返回 -1。要获取错误代码,请使用 GetLastError() 函数。

如果通过文件名获取属性时指定了文件夹,无论何种情况,函数都将返回错误 5018 (ERR_MQL_FILE_IS_DIRECTORY),尽管返回值将是正确的。

注意

该函数始终会改变错误代码。成功完成后,错误代码将重置为 NULL。

示例:

//--- display the window of input parameters when launching the script
#property script_show_inputs
//--- input parameters
input string InpFileName="data.csv";
input string InpDirectoryName="SomeFolder";
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   string path=InpDirectoryName+"//"+InpFileName;
   long   l=0;
//--- open the file
   ResetLastError();
   int handle=FileOpen(path,FILE_READ|FILE_CSV);
   if(handle!=INVALID_HANDLE)
     {
      //--- print all information about the file
      Print(InpFileName," file info:");
      FileInfo(handle,FILE_EXISTS,l,"bool");
      FileInfo(handle,FILE_CREATE_DATE,l,"date");
      FileInfo(handle,FILE_MODIFY_DATE,l,"date");
      FileInfo(handle,FILE_ACCESS_DATE,l,"date");
      FileInfo(handle,FILE_SIZE,l,"other");
      FileInfo(handle,FILE_POSITION,l,"other");
      FileInfo(handle,FILE_END,l,"bool");
      FileInfo(handle,FILE_IS_COMMON,l,"bool");
      FileInfo(handle,FILE_IS_TEXT,l,"bool");
      FileInfo(handle,FILE_IS_BINARY,l,"bool");
      FileInfo(handle,FILE_IS_CSV,l,"bool");
      FileInfo(handle,FILE_IS_ANSI,l,"bool");
      FileInfo(handle,FILE_IS_READABLE,l,"bool");
      FileInfo(handle,FILE_IS_WRITABLE,l,"bool");
      //--- close the file
      FileClose(handle);
     }
   else
      PrintFormat("%s file is not opened, ErrorCode = %d",InpFileName,GetLastError());
  }
//+------------------------------------------------------------------+
//| Display the value of the file property                           |
//+------------------------------------------------------------------+
void FileInfo(const int handle,const ENUM_FILE_PROPERTY_INTEGER id,
              long l,const string type)
  {
//--- receive the property value
   ResetLastError();
   if((l=FileGetInteger(handle,id))!=-1)
     {
      //--- the value received, display it in the correct format
      if(!StringCompare(type,"bool"))
         Print(EnumToString(id)," = ",l ? "true" : "false");
      if(!StringCompare(type,"date"))
         Print(EnumToString(id)," = ",(datetime)l);
      if(!StringCompare(type,"other"))
         Print(EnumToString(id)," = ",l);
     }
   else
      Print("Error, Code = ",GetLastError());
  }

另请参阅

文件操作, 文件属性

最后更新于