【MT4学习】MQL4语言基础语法12-文件处理函数 [File Functions]

1
回复
5867
查看
[复制链接]

63

主题

36

回帖

1915

积分

管理员

积分
1915
来源: 2019-7-31 06:29:35 显示全部楼层 |阅读模式
void FileClose(int handle)
关闭正在已经打开的文件.
:: 输入参数
handle - FileOpen()返回的句柄
示例:
  1. int
  2. handle=FileOpen("filename", FILE_CSV|FILE_READ);

  3. if(handle>0)

  4. {

  5. // working with file ...

  6. FileClose(handle);

  7. }
复制代码



void FileDelete(stringfilename)
删除文件,如果发生错误可以通过GetLastError()来查询
注:你只能操作terminal_dir\experts\files目录下的文件
:: 输入参数
filename - 目录和文件名
示例:
  1. // file
  2. my_table.csv will be deleted from terminal_dir\experts\files directory

  3. int lastError;

  4. FileDelete("my_table.csv");

  5. lastError=GetLastError();

  6. if(laseError!=ERR_NOERROR)

  7. {

  8. Print("An error ocurred while (",lastError,") deleting file
  9. my_table.csv");

  10. return(0);

  11. }
复制代码



void FileFlush(inthandle)
将缓存中的数据刷新到磁盘上去
:: 输入参数
handle - FileOpen()返回的句柄
示例:
  1. int
  2. bars_count=Bars;

  3. int handle=FileOpen("mydat.csv",FILE_CSV|FILE_WRITE);

  4. if(handle>0)

  5. {

  6. FileWrite(handle,
  7. "#","OPEN","CLOSE","HIGH","LOW");

  8. for(int i=0;i<BARS_COUNT;I++)

  9. FileWrite(handle, i+1,Open[i],Close[i],High[i], Low[i]);

  10. FileFlush(handle);

  11. ...

  12. for(int i=0;i<BARS_COUNT;I++)

  13. FileWrite(handle, i+1,Open[i],Close[i],High[i], Low[i]);

  14. FileClose(handle);

  15. }
复制代码



bool FileIsEnding(inthandle)
检查是否到了文件尾.
:: 输入参数
handle - FileOpen()返回的句柄
示例:
  1. if(FileIsEnding(h1))

  2. {

  3. FileClose(h1);

  4. return(false);

  5. }
复制代码



bool FileIsLineEnding(int handle)
检查行是否到了结束
:: 输入参数
handle - FileOpen()返回的句柄
示例:
  1. if(FileIsLineEnding(h1))

  2. {

  3. FileClose(h1);

  4. return(false);

  5. }
复制代码



int FileOpen( stringfilename, int mode, int delimiter=';')
打开文件,如果失败返回值小于1,可以通过GetLastError()获取错误
注:只能操作terminal_dir\experts\files目录的文件
:: 输入参数
filename - 目录文件名
mode - 打开模式 FILE_BIN,FILE_CSV, FILE_READ, FILE_WRITE.
delimiter - CSV型打开模式用的分割符,默认为分号(;).
示例:
  1. int
  2. handle;

  3. handle=FileOpen("my_data.csv",FILE_CSV|FILE_READ,';');

  4. if(handle<1)

  5. {

  6. Print("File my_data.dat not found, the last error is ",
  7. GetLastError());

  8. return(false);

  9. }
复制代码



int FileOpenHistory(string filename, int mode, int delimiter=';')
打开历史数据文件,如果失败返回值小于1,可以通过GetLastError()获取错误
:: 输入参数
filename - 目录文件名
mode - 打开模式 FILE_BIN,FILE_CSV, FILE_READ, FILE_WRITE.
delimiter - CSV型打开模式用的分割符,默认为分号(;).
示例:
i
  1. nt
  2. handle=FileOpenHistory("USDX240.HST",FILE_BIN|FILE_WRITE);

  3. if(handle<1)

  4. {

  5. Print("Cannot create file USDX240.HST");

  6. return(false);

  7. }

  8. // work with file

  9. // ...

  10. FileClose(handle);
复制代码



int FileReadArray( inthandle, object& array[], int start, int count)
将二进制文件读取到数组中,返回读取的条数,可以通过GetLastError()获取错误
注:在读取之前要调整好数组大小
:: 输入参数
handle - FileOpen()返回的句柄
array[] - 写入的数组
start - 在数组中存储的开始点
count - 读取多少个对象
示例:
  1. int
  2. handle;

  3. double varray[10];

  4. handle=FileOpen("filename.dat", FILE_BIN|FILE_READ);

  5. if(handle>0)

  6. {

  7. FileReadArray(handle, varray, 0, 10);

  8. FileClose(handle);

  9. }
复制代码



double FileReadDouble(int handle, int size=DOUBLE_VALUE)
从文件中读取浮点型数据,数字可以是8byte的double型或者是4byte的float型。
:: 输入参数
handle - FileOpen()返回的句柄
size - 数字个是大小,DOUBLE_VALUE(8bytes) 或者FLOAT_VALUE(4 bytes).
示例:
  1. int
  2. handle;

  3. double value;

  4. handle=FileOpen("mydata.dat",FILE_BIN);

  5. if(handle>0)

  6. {

  7. value=FileReadDouble(handle,DOUBLE_VALUE);

  8. FileClose(handle);

  9. }
复制代码



int FileReadInteger( inthandle, int size=LONG_VALUE)
从文件中读取整形型数据,数字可以是1,2,4byte的长度
:: 输入参数
handle - FileOpen()返回的句柄
size - 数字个是大小,CHAR_VALUE(1byte), SHORT_VALUE(2 bytes) 或者LONG_VALUE(4 bytes).
示例:
  1. int
  2. handle;

  3. int value;

  4. handle=FileOpen("mydata.dat", FILE_BIN|FILE_READ);

  5. if(handle>0)

  6. {

  7. value=FileReadInteger(h1,2);

  8. FileClose(handle);

  9. }
复制代码



double FileReadNumber(int handle)
从文件中读取数字,只能在CSV里使用
:: 输入参数
handle - FileOpen()返回的句柄
示例:
  1. int
  2. handle;

  3. int value;

  4. handle=FileOpen("filename.csv", FILE_CSV, ';');

  5. if(handle>0)

  6. {

  7. value=FileReadNumber(handle);

  8. FileClose(handle);

  9. }
复制代码



string FileReadString(int handle, int length=0)
从文件中读取字符串
:: 输入参数
handle - FileOpen()返回的句柄
length - 读取字符串长度
示例:
i
  1. nt
  2. handle;

  3. string str;

  4. handle=FileOpen("filename.csv", FILE_CSV|FILE_READ);

  5. if(handle>0)

  6. {

  7. str=FileReadString(handle);

  8. FileClose(handle);

  9. }
复制代码



bool FileSeek( inthandle, int offset, int origin)
移动指针移动到某一点,如果成功返回true
:: 输入参数
handle - FileOpen()返回的句柄
offset - 设置的原点
origin - SEEK_CUR从当前位置开始 SEEK_SET从文件头部开始SEEK_END 从文件尾部开始
示例:
  1. int
  2. handle=FileOpen("filename.csv", FILE_CSV|FILE_READ, ';');

  3. if(handle>0)

  4. {

  5. FileSeek(handle, 10, SEEK_SET);

  6. FileReadInteger(handle);

  7. FileClose(handle);

  8. handle=0;

  9. }
复制代码



int FileSize( inthandle)
返回文件大小
:: 输入参数
handle - FileOpen()返回的句柄
示例:
  1. int
  2. handle;

  3. int size;

  4. handle=FileOpen("my_table.dat", FILE_BIN|FILE_READ);

  5. if(handle>0)

  6. {

  7. size=FileSize(handle);

  8. Print("my_table.dat size is ", size, " bytes");

  9. FileClose(handle);

  10. }
复制代码



int FileTell( inthandle)
返回文件读写指针当前的位置
:: 输入参数
handle - FileOpen()返回的句柄
示例:
  1. int
  2. handle;

  3. int pos;

  4. handle=FileOpen("my_table.dat", FILE_BIN|FILE_READ);

  5. // reading some data

  6. pos=FileTell(handle);

  7. Print("current position is ", pos);
复制代码



int FileWrite( inthandle, ... )
向文件写入数据
:: 输入参数
handle - FileOpen()返回的句柄
... - 写入的数据
示例:
  1. int
  2. handle;

  3. datetime orderOpen=OrderOpenTime();

  4. handle=FileOpen("filename", FILE_CSV|FILE_WRITE, ';');

  5. if(handle>0)

  6. {

  7. FileWrite(handle, Close[0], Open[0], High[0], Low[0], TimeToStr(orderOpen));

  8. FileClose(handle);

  9. }
复制代码



int FileWriteArray( inthandle, object array[], int start, int count)
向文件写入数组
:: 输入参数
handle - FileOpen()返回的句柄
array[] - 要写入的数组
start - 写入的开始点
count - 写入的项目数
示例:
  1. int
  2. handle;

  3. double BarOpenValues[10];

  4. // copy first ten bars to the array

  5. for(int i=0;i<10; i++)

  6. BarOpenValues[i]=Open[i];

  7. // writing array to the file

  8. handle=FileOpen("mydata.dat", FILE_BIN|FILE_WRITE);

  9. if(handle>0)

  10. {

  11. FileWriteArray(handle, BarOpenValues, 3, 7); // writing last 7 elements

  12. FileClose(handle);

  13. }
复制代码



int FileWriteDouble( inthandle, double value, int size=DOUBLE_VALUE)
向文件写入浮点型数据
:: 输入参数
handle - FileOpen()返回的句柄
value - 要写入的值
size - 写入的格式,DOUBLE_VALUE(8 bytes, default)或FLOAT_VALUE(4 bytes).
示例:
  1. int
  2. handle;

  3. double var1=0.345;

  4. handle=FileOpen("mydata.dat", FILE_BIN|FILE_WRITE);

  5. if(handle<1)

  6. {

  7. Print("can't open file error-",GetLastError());

  8. return(0);

  9. }

  10. FileWriteDouble(h1, var1, DOUBLE_VALUE);

  11. //...

  12. FileClose(handle);
复制代码



int FileWriteInteger(int handle, int value, int size=LONG_VALUE)
向文件写入整型数据
:: 输入参数
handle - FileOpen()返回的句柄
value - 要写入的值
size - 写入的格式,CHAR_VALUE(1 byte),SHORT_VALUE (2 bytes),LONG_VALUE (4 bytes, default).
示例:
  1. int
  2. handle;

  3. int value=10;

  4. handle=FileOpen("filename.dat", FILE_BIN|FILE_WRITE);

  5. if(handle<1)

  6. {

  7. Print("can't open file error-",GetLastError());

  8. return(0);

  9. }

  10. FileWriteInteger(handle, value, SHORT_VALUE);

  11. //...

  12. FileClose(handle);
复制代码



int FileWriteString( inthandle, string value, int length)
向文件写入字符串数据
:: 输入参数
handle - FileOpen()返回的句柄
value - 要写入的值
length - 写入的字符长度
   MT4编程课程.png
示例:
  1. int
  2. handle;

  3. string str="some string";

  4. handle=FileOpen("filename.bin", FILE_BIN|FILE_WRITE);

  5. if(handle<1)

  6. {

  7. Print("can't open file error-",GetLastError());

  8. return(0);

  9. }

  10. FileWriteString(h1, str, 8);

  11. FileClose(handle);
复制代码



回复

使用道具 举报

19

主题

36

回帖

201

积分

中级会员

积分
201
2019-7-31 08:02:52 来自手机 显示全部楼层
学习学习,感谢分享
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 免费注册
关注微信