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());
}
|
|