路路发智能交易研发中心

 找回密码
 立即注册
查看: 5828|回复: 0

MT4智能交易编程教程-FileOpen函数

    [复制链接]

管理员

MT4软件工程师

Rank: 9Rank: 9Rank: 9

积分
6521
帖子
2771
主题
2761
QQ
发表于 2014-3-27 10:08:57 | 显示全部楼层 |阅读模式
MT4智能交易编程教程-FileOpen函数

函数打开文件的指定名称和标签。
int  FileOpen(
   string  file_name,           // 文件名
   int     open_flags,          // 标记的组合
   short   delimiter='\t',      // 定界符
   uint    codepage=CP_ACP      // 代码页
   );


参量
file_name
[in]  文件名称包括子文件夹,如果文件为编辑开放,就会建立子文件夹。
open_flags
[in] 标记的组合 决定文件夹的操作方式,标签如下定义
FILE_READ为阅读展开
FILE_WRITE 为编辑展开
FILE_BIN 二次编辑阅读模式(不会从字符串到字符串转换)
FILE_CSV csv类型文件(所有记录项目转换成统一码字符串或者ansi类型,被定界符分开)
FILE_TXT 简单文本文件(与csv相同,但不考虑定界符)
FILE_ANSI ANSI类型线(单一字节交易品种)
FILE_UNICODE UNICODE类型线(双精度函数字符)
FILE_SHARE_READ 从各自程序中共享阅读
FILE_SHARE_WRITE 从各自程序中共享编辑
FILE_COMMON 所有客户端的共享文件夹中的文件位置
delimiter='\t'
[in]  在txt或者csv文件中使用分隔符的值,如果csv文件标识符不是指定的,默认到标签,如果txt文件标识符不是制定的,就不使用分隔符。如果分隔符清晰建立成0,不使用分隔符。
codepage=CP_ACP
[in]  代码页的值,最多使用 代码页 提供占用常量。
返回值
如果文件成功打开,函数返回文件处理,然后访问文件数据,失败后返回 INVALID_HANDLE。
注释
  出于安全考虑,工作文件必须严格由MQL5语言管理。使用MQL5实     施文件操作的文件意味着,不能在文件沙箱外。
文件在客户端子文件 MQL5\files (或者каталог_агента_тестирования\MQL5\files)中打开,如果FILE_COMMON在制定标签中,文件在所有MT5客户端的共享文件中打开。
"Named pipes" can be opened according to the following rules:
·Pipe name is a string, which should have the following look: "\\servername\pipe\pipename", where servername - server name in the network, while pipename is a pipe name. If the pipes are used on the same computer, the server name can be omitted but a point should be inserted instead of it: "\\.\pipe\pipename". A client trying to connect the pipe should know its name.
·FileFlush() and FileSeek() should be called to the beginning of a file between sequential operations of reading from the pipe and writing to it.
A special symbol '\' is used in shown strings. Therefore, '\' should be doubled when writing a name in MQL5 application. It means that the above example should have the following look in the code: "\\\\servername\\pipe\\pipename".
More information about working with named pipes can be found in the article "Communicating With MetaTrader 5 Using Named Pipes Without Using DLLs"
示例:
//+------------------------------------------------------------------+
//| 启动函数的脚本程序                                                  |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- 错误的打开文件方法
   string terminal_data_path=TerminalInfoString(TERMINAL_DATA_PATH);
   string filename=terminal_data_path+"\\MQL5\\Files\\"+"fractals.csv";
   int filehandle=FileOpen(filename,FILE_WRITE|FILE_CSV);
   if(filehandle<0)
     {
      Print("Failed to open the file by the absolute path ");
      Print("Error code ",GetLastError());
     }

//--- “文件沙盒效应”中正确的工作方法
   ResetLastError();
   filehandle=FileOpen("fractals.csv",FILE_WRITE|FILE_CSV);
   if(filehandle!=INVALID_HANDLE)
     {
      FileWrite(filehandle,TimeCurrent(),Symbol(),PERIOD_CURRENT);
      FileClose(filehandle);
      Print("FileOpen OK");
     }
   else Print("Operation FileOpen failed, error ",GetLastError());
//---在MQL5\Files\中创建附着目录的另一个示例
   string subfolder="Research";
   filehandle=FileOpen(subfolder+"\\fractals.txt",FILE_WRITE|FILE_CSV);
      if(filehandle!=INVALID_HANDLE)
     {
      FileWrite(filehandle,TimeCurrent(),Symbol(),PERIOD_CURRENT);
      FileClose(filehandle);
      Print("The file most be created in the folder "+terminal_data_path+"\\"+subfolder);
     }
   else Print("File open failed, error ",GetLastError());
  }






外汇智能交易,成就财富梦想!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则


MT4编程培训|EA定制开发|QQ在线咨询|路路发智能交易研发中心

GMT+8, 2024-5-2 05:59 , Processed in 0.181875 second(s), 26 queries .

© 2009-2022 520EA.com EA668.com

快速回复 返回顶部 返回列表