FileTell

文件返回打开文件指针的当前仓位。

ulong  FileTell(
   int  file_handle    // 文件句柄
   );

参量

file_handle

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

返回值

从文件开始,文件说明符的当前仓位字节数。

注释

调用GetLastError()函数获得错误信息。

示例 :

//+------------------------------------------------------------------+
//|                                                Demo_FileTell.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;
      //--- 字符串起始点的转换位置
      FileSeek(file_handle,pos[ind],SEEK_SET);
      //--- 阅读和打印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);
  }