打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
MT4编程参考-文件函数

 文件处理函数 [File Functions]

void FileClose(int handle)
关闭正在已经打开的文件.

:: 输入参数
handle - FileOpen()
返回的句柄

示例:

int handle=FileOpen("filename", FILE_CSV|FILE_READ);
if(handle>0)
{
// working with file ...
FileClose(handle);
}


void FileDelete(stringfilename)
删除文件,如果发生错误可以通过GetLastError()来查询
:你只能操作terminal_direxpertsfiles目录下的文件

:: 输入参数
filename -
 目录和文件名

示例:

// file my_table.csv will be deleted from terminal_direxpertsfilesdirectory
int lastError;
FileDelete("my_table.csv");
lastError=GetLastError();
if(laseError!=ERR_NOERROR)
{
Print("An error ocurred while (",lastError,") deleting filemy_table.csv");
return(0);
}


void FileFlush(int handle)
将缓存中的数据刷新到磁盘上去

:: 输入参数
handle - FileOpen()
返回的句柄

示例:

int bars_count=Bars;
int handle=FileOpen("mydat.csv",FILE_CSV|FILE_WRITE);
if(handle>0)
{
FileWrite(handle, "#","OPEN","CLOSE","HIGH","LOW");
for(int i=0;i
FileWrite(handle, i+1,Open[i],Close[i],High[i], Low[i]);
FileFlush(handle);
...
for(int i=0;i
FileWrite(handle, i+1,Open[i],Close[i],High[i], Low[i]);
FileClose(handle);
}


bool FileIsEnding(inthandle)
检查是否到了文件尾.

:: 输入参数
handle - FileOpen()
返回的句柄

示例:

if(FileIsEnding(h1))
{
FileClose(h1);
return(false);
}


bool FileIsLineEnding( inthandle)
检查行是否到了结束

:: 输入参数
handle - FileOpen()
返回的句柄

示例:

if(FileIsLineEnding(h1))
{
FileClose(h1);
return(false);
}


int FileOpen( string filename, int mode, intdelimiter=';')
打开文件,如果失败返回值小于1,可以通过GetLastError()获取错误
:只能操作terminal_direxpertsfiles目录的文件

:: 输入参数
filename -
 目录文件名
mode -
 打开模式 FILE_BIN,FILE_CSV, FILE_READ, FILE_WRITE.
delimiter - CSV
型打开模式用的分割符,默认为分号(;).

示例:

int handle;
handle=FileOpen("my_data.csv",FILE_CSV|FILE_READ,';');
if(handle<1)
{
Print("File my_data.dat not found, the last error is ",GetLastError());
return(false);
}


int FileOpenHistory( string filename, int mode, intdelimiter=';')
打开历史数据文件,如果失败返回值小于1,可以通过GetLastError()获取错误

:: 输入参数
filename -
 目录文件名
mode -
 打开模式 FILE_BIN,FILE_CSV, FILE_READ, FILE_WRITE.
delimiter - CSV
型打开模式用的分割符,默认为分号(;).

示例:

inthandle=FileOpenHistory("USDX240.HST",FILE_BIN|FILE_WRITE);
if(handle<1)
{
Print("Cannot create file USDX240.HST");
return(false);
}
// work with file
// ...
FileClose(handle);


int FileReadArray( int handle, object& array[], int start, intcount)
将二进制文件读取到数组中,返回读取的条数,可以通过GetLastError()获取错误
:在读取之前要调整好数组大小

:: 输入参数
handle - FileOpen()
返回的句柄
array[] -
 写入的数组
start -
 在数组中存储的开始点
count -
 读取多少个对象

示例:

int handle;
double varray[10];
handle=FileOpen("filename.dat", FILE_BIN|FILE_READ);
if(handle>0)
{
FileReadArray(handle, varray, 0, 10);
FileClose(handle);
}


double FileReadDouble( int handle, intsize=DOUBLE_VALUE)
从文件中读取浮点型数据,数字可以是8bytedouble型或者是4bytefloat型。

:: 输入参数
handle - FileOpen()
返回的句柄
size -
 数字个是大小,DOUBLE_VALUE(8bytes) 或者 FLOAT_VALUE(4bytes).

示例:

int handle;
double value;
handle=FileOpen("mydata.dat",FILE_BIN);
if(handle>0)
{
value=FileReadDouble(handle,DOUBLE_VALUE);
FileClose(handle);
}


int FileReadInteger( int handle, intsize=LONG_VALUE)
从文件中读取整形型数据,数字可以是1,2,4byte的长度

:: 输入参数
handle - FileOpen()
返回的句柄
size -
 数字个是大小,CHAR_VALUE(1byte), SHORT_VALUE(2 bytes) 或者 LONG_VALUE(4bytes).

示例:

int handle;
int value;
handle=FileOpen("mydata.dat", FILE_BIN|FILE_READ);
if(handle>0)
{
value=FileReadInteger(h1,2);
FileClose(handle);
}


double FileReadNumber( inthandle)
从文件中读取数字,只能在CSV里使用

:: 输入参数
handle - FileOpen()
返回的句柄

示例:

int handle;
int value;
handle=FileOpen("filename.csv", FILE_CSV, ';');
if(handle>0)
{
value=FileReadNumber(handle);
FileClose(handle);
}


string FileReadString( int handle, intlength=0)
从文件中读取字符串

:: 输入参数
handle - FileOpen()
返回的句柄
length -
 读取字符串长度

示例:

int handle;
string str;
handle=FileOpen("filename.csv", FILE_CSV|FILE_READ);
if(handle>0)
{
str=FileReadString(handle);
FileClose(handle);
}


bool FileSeek( int handle, int offset, intorigin)
移动指针移动到某一点,如果成功返回true

:: 输入参数
handle - FileOpen()
返回的句柄
offset -
 设置的原点
origin - SEEK_CUR
从当前位置开始 SEEK_SET从文件头部开始 SEEK_END 从文件尾部开始

示例:

int handle=FileOpen("filename.csv", FILE_CSV|FILE_READ, ';');
if(handle>0)
{
FileSeek(handle, 10, SEEK_SET);
FileReadInteger(handle);
FileClose(handle);
handle=0;
}


int FileSize( int handle)
返回文件大小

:: 输入参数
handle - FileOpen()
返回的句柄

示例:

int handle;
int size;
handle=FileOpen("my_table.dat", FILE_BIN|FILE_READ);
if(handle>0)
{
size=FileSize(handle);
Print("my_table.dat size is ", size, " bytes");
FileClose(handle);
}


int FileTell( int handle)
返回文件读写指针当前的位置

:: 输入参数
handle - FileOpen()
返回的句柄

示例:

int handle;
int pos;
handle=FileOpen("my_table.dat", FILE_BIN|FILE_READ);
// reading some data
pos=FileTell(handle);
Print("current position is ", pos);


int FileWrite( int handle, ...)
向文件写入数据

:: 输入参数
handle - FileOpen()
返回的句柄
... -
 写入的数据

示例:

int handle;
datetime orderOpen=OrderOpenTime();
handle=FileOpen("filename", FILE_CSV|FILE_WRITE, ';');
if(handle>0)
{
FileWrite(handle, Close[0], Open[0], High[0], Low[0],TimeToStr(orderOpen));
FileClose(handle);
}


int FileWriteArray( int handle, object array[], int start, intcount)
向文件写入数组

:: 输入参数
handle - FileOpen()
返回的句柄
array[] -
 要写入的数组
start -
 写入的开始点
count -
 写入的项目数

示例:

int handle;
double BarOpenValues[10];
// copy first ten bars to the array
for(int i=0;i<10; i++)
BarOpenValues[i]=Open[i];
// writing array to the file
handle=FileOpen("mydata.dat", FILE_BIN|FILE_WRITE);
if(handle>0)
{
FileWriteArray(handle, BarOpenValues, 3, 7); // writing last 7elements
FileClose(handle);
}


int FileWriteDouble( int handle, double value, intsize=DOUBLE_VALUE)
向文件写入浮点型数据

:: 输入参数
handle - FileOpen()
返回的句柄
value -
 要写入的值
size -
 写入的格式,DOUBLE_VALUE(8 bytes, default)FLOAT_VALUE(4 bytes).

示例:

int handle;
double var1=0.345;
handle=FileOpen("mydata.dat", FILE_BIN|FILE_WRITE);
if(handle<1)
{
Print("can't open file error-",GetLastError());
return(0);
}
FileWriteDouble(h1, var1, DOUBLE_VALUE);
//...
FileClose(handle);


int FileWriteInteger( int handle, int value, intsize=LONG_VALUE)
向文件写入整型数据

:: 输入参数
handle - FileOpen()
返回的句柄
value -
 要写入的值
size -
 写入的格式,CHAR_VALUE(1 byte),SHORT_VALUE (2 bytes),LONG_VALUE (4 bytes,default).

示例:

int handle;
int value=10;
handle=FileOpen("filename.dat", FILE_BIN|FILE_WRITE);
if(handle<1)
{
Print("can't open file error-",GetLastError());
return(0);
}
FileWriteInteger(handle, value, SHORT_VALUE);
//...
FileClose(handle);


int FileWriteString( int handle, string value, intlength)
向文件写入字符串数据

:: 输入参数
handle - FileOpen()
返回的句柄
value -
 要写入的值
length -
 写入的字符长度

示例:

int handle;
string str="some string";
handle=FileOpen("filename.bin", FILE_BIN|FILE_WRITE);
if(handle<1)
{
Print("can't open file error-",GetLastError());
return(0);
}
FileWriteString(h1, str, 8);
FileClose(handle);

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
MQL4编程:文件操作函数(三)
asp文件操作函数详解
强化学习中的随机决策森林
内存句柄HGLOBAL
[i] 3 点差记录
MSDN中回调函数的讲解及其C#例子:用委托实现回调函数
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服