FileSeek

通过指定仓位相关字节数,函数移动文件指标的仓位。

bool  FileSeek(
   int                  file_handle,     // 文件句柄
   long                 offset,          // 字节
   ENUM_FILE_POSITION   origin           // 参考位置
   );

参量

file_handle

[in]  通过 FileOpen()返回文件描述符。

offset

[in] 字节转换(也许获取负值)。

origin

[in] 移位起始点。可以是 ENUM_FILE_POSITION值中一个。

返回值

如果成功返回true, 否则false。调用GetLastError()函数获得错误信息。

注释

如果执行FileSeek()函数结果导致负值转换(超过文件的水平边界),文件指标在文件起始运行时建立。

如果仓位的建立超过文件的右边界(比文件大),接下来的录入文件不是从文件末尾开始,而是从仓位开始。不确定值为先前文件末尾和新建仓位编辑。

示例 :

//+------------------------------------------------------------------+
//|                                                Demo_FileSeek.mq5 |
//|                        Copyright 2013, MetaQuotes Software Corp. |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
//--- 启动脚本时显示输入参数的窗口
#property script_show_inputs
//--- 输入参数
input string InpFileName="file.txt";    // 文件名称
input string InpDirectoryName="Data";   // 目录名称
input int    InpEncodingType=FILE_ANSI// ANSI=32 或 UNICODE=64
//+------------------------------------------------------------------+
//| 脚本程序启动函数                                                   |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- 指定生成随机数字的变量值
   _RandomSeed=GetTickCount();
//--- 字符串起始点位置的变量
   ulong pos[];
   int   size;
//--- 重置错误值
   ResetLastError();
//--- 打开文件
   int file_handle=FileOpen(InpDirectoryName+"//"+InpFileName,FILE_READ|FILE_TXT|InpEncodingType);
   if(file_handle!=INVALID_HANDLE)
     {
      PrintFormat("%s file is available for reading",InpFileName);
      //--- 接收文件中每个字符串的起始位置
      GetStringPositions(file_handle,pos);
      //--- 定义文件中的字符串数量
      size=ArraySize(pos);
      if(!size)
        {
         //--- 如果文件没有字符串,则停止
         PrintFormat("%s file is empty!",InpFileName);
         FileClose(file_handle);
         return;
        }
      //--- 随机选择一个字符串数
      int ind=MathRand()%size;
      //--- 字符串起始点的转换位置
      if(FileSeek(file_handle,pos[ind],SEEK_SET)==true)
        {
      //--- 阅读和打印ind数字的字符串
         PrintFormat("String text with %d number: \"%s\"",ind,FileReadString(file_handle));
        }
      //--- 关闭文件
      FileClose(file_handle);
      PrintFormat("%s file is closed",InpFileName);
     }
   else
      PrintFormat("Failed to open %s file, Error code = %d",InpFileName,GetLastError());
  }
//+-------------------------------------------------------------------------------+
//| 函数定义了文件中每个字符串的起始点并在arr数组放置它                                  |
//|                                                                               |
//+-------------------------------------------------------------------------------+
void GetStringPositions(const int handle,ulong &arr[])
  {
//--- 默认数组大小
   int def_size=127;
//--- 为数组分配内存
   ArrayResize(arr,def_size);
//--- 字符串计数器
   int i=0;
//--- 如果这不是文件结尾,那么至少有一个字符串
   if(!FileIsEnding(handle))
     {
      arr[i]=FileTell(handle);
      i++;
     }
   else
      return// 文件为空,退出
//--- 根据编码定义字节转换
   int shift;
   if(FileGetInteger(handle,FILE_IS_ANSI))
      shift=1;
   else
      shift=2;
//--- 通过循环中的字符串
   while(1)
     {
      //--- 阅读字符串
      FileReadString(handle);
      //--- 检查文件结尾
      if(!FileIsEnding(handle))
        {
         //--- 存储下一个字符串的位置
         arr[i]=FileTell(handle)+shift;
         i++;
         //--- 如果超过增加数组大小
         if(i==def_size)
           {
            def_size+=def_size+1;
            ArrayResize(arr,def_size);
           }
        }
      else
         break// 文件结尾,退出
     }
//--- 定义数组的实际大小
   ArrayResize(arr,i);
  }