请观看如何免费下载自动交易
请在Facebook上找到我们!
加入我们粉丝页
有趣的脚本?
因此发布一个链接 -
让其他人评价
喜欢这个脚本? 在MetaTrader 5客户端尝试它
显示:
1440
等级:
(33)
已发布:
2013.10.25 08:45
已更新:
2016.11.22 07:33
\MQL5\Files\Data\
file.txt (0.1 KB) 预览
需要基于此代码的EA交易或指标吗?请在自由职业者服务中订购 进入自由职业者服务

这段脚本打开一个文本文件, 分析文件中字符串的起始位置,并显示字符串加一个随机数。文件中每个字符串的起始位置用 GetStringPositions() 函数获得,并存于数组中。函数搜索字符串时要考虑文件编码类型,其值可由 "InpEncodingType" 输入参数指定。

除了使用 FileTell() 函数, 脚本还用到 FileGetInteger() 函数来获取编码类型, FileIsEnding() 函数来检测文件结束以及当用随机数显示字符串时,用 FileSeek() 函数来移动文件指针位置。

代码:

//--- 当脚本启动时显示输入参数窗口
#property script_show_inputs
//--- 输入参数
input string InpFileName="file.txt";    // 文件名
input string InpDirectoryName="Data";   // 目录名
input int    InpEncodingType=FILE_ANSI; // ANSI=32 or UNICODE=64
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
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 文件可用于读取",InpFileName);
      //--- 接收文件中每个字符串位置
      GetStringPositions(file_handle,pos);
      //--- 文件中字符串数量
      size=ArraySize(pos);
      if(!size)
        {
         //--- 如果没有字符串,停止
         PrintFormat("%s 文件空!",InpFileName);
         FileClose(file_handle);
         return;
        }
      //--- 随机选择字符串号码
      int ind=MathRand()%size;
      //--- 根据字符串号码平移文件指针
      FileSeek(file_handle,pos[ind],SEEK_SET);
      //--- 读并打印号码指定的字符串
      PrintFormat("字符串号码 %d : \"%s\"",ind,FileReadString(file_handle));
      //--- 关闭文件
      FileClose(file_handle);
      PrintFormat("%s 文件关闭",InpFileName);
     }
   else
      PrintFormat("打开文件失败 %s , 错误代码 = %d",InpFileName,GetLastError());
  }
//+-------------------------------------------------------------------------------+
//| The function defines starting points for each of the strings in the file and  |
//| places them in arr array                                                      |
//+-------------------------------------------------------------------------------+
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);
  }

由MetaQuotes Ltd译自俄语
原代码: https://www.mql5.com/ru/code/1634

演示_FileSize 演示_FileSize

这段脚本简单示例如何使用 FileSize() 函数

演示_FileReadDouble 演示_FileReadDouble

这段脚本简单示例如何使用 FileReadDouble() 函数

演示_FileWriteInteger 演示_FileWriteInteger

这段脚本简单示例如何使用 FileWriteInteger() 函数

演示_FileReadInteger 演示_FileReadInteger

这段脚本简单示例如何使用 FileReadInteger() 函数