FileReadInteger
FileReadInteger
此函数根据指定的字节数,从文件指针的当前位置读取 int、short 或 char 类型的值。
int FileReadInteger(
int file_handle, // File handle
int size=INT_VALUE // Size of an integer in bytes
);参数
- file_handle
[in] 由 FileOpen() 返回的文件描述符。
- size=INT_VALUE
[in] 需要读取的字节数(最多包含 4 个字节)。相应的常量如下:CHAR_VALUE = 1,SHORT_VALUE = 2,INT_VALUE(LONG_VALUE) = 4,因此该函数可以读取 char、short、int 或 long 类型的值。
返回值
int 类型的值。此函数的结果必须显式转换为目标类型,即需要读取的数据类型。由于返回的是 int 类型的值,因此可以轻松将其转换为任何整数值。文件指针会向后移动读取的字节数。
注意
当读取的字节数少于 4 个时,接收的结果总是正数。如果读取了 1 或 2 个字节,可以通过显式转换为 char(1 个字节)或 short(2 个字节)来确定数的符号。获取 3 个字节数的符号并不简单,因为不存在相应的底层类型。
示例(此处使用了执行 FileWriteInteger() 函数后得到的文件)
//+------------------------------------------------------------------+
//| Demo_FileReadInteger.mq4 |
//| Copyright 2014, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property strict
#property indicator_chart_window
#property indicator_buffers 1
//---- plot Label1
#property indicator_label1 "Trends"
#property indicator_type1 DRAW_SECTION
#property indicator_color1 clrRed
#property indicator_style1 STYLE_SOLID
#property indicator_width1 2
//--- parameters for data reading
input string InpFileName="Trend.bin"; // File name
input string InpDirectoryName="Data"; // Folder name
//--- global variables
int ind=0;
int size=0;
datetime time_buff[];
//--- indicator buffers
double buff[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
int def_size=100;
//--- allocate memory for the array
ArrayResize(time_buff,def_size);
//--- open the file
ResetLastError();
int file_handle=FileOpen(InpDirectoryName+"//"+InpFileName,FILE_READ|FILE_BIN);
if(file_handle!=INVALID_HANDLE)
{
PrintFormat("%s file is available for reading",InpFileName);
PrintFormat("File path: %s\\Files\\",TerminalInfoString(TERMINAL_DATA_PATH));
//--- additional variables
int arr_size;
uchar arr[];
//--- read data from the file
while(!FileIsEnding(file_handle))
{
//--- find out how many bytes are used for writing the time
arr_size=FileReadInteger(file_handle,INT_VALUE);
ArrayResize(arr,arr_size);
for(int i=0;i<arr_size;i++)
arr[i]=(char)FileReadInteger(file_handle,CHAR_VALUE);
//--- store the time value
time_buff[size]=StringToTime(CharArrayToString(arr));
size++;
//--- increase the sizes of the arrays if they are filled
if(size==def_size)
{
def_size+=100;
ArrayResize(time_buff,def_size);
}
}
//--- close the file
FileClose(file_handle);
PrintFormat("Data is read, %s file is closed",InpFileName);
}
else
{
PrintFormat("Failed to open %s file, Error code = %d",InpFileName,GetLastError());
return(INIT_FAILED);
}
//--- bind the array to the indicator buffer
SetIndexBuffer(0,buff,INDICATOR_DATA);
//---- set the indicator values that will not be visible on the chart
SetIndexEmptyValue(0,0.0);
//--- set indexing as timeseries
ArraySetAsSeries(time_buff,true);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
ArraySetAsSeries(time,true);
ArraySetAsSeries(close,true);
//--- the loop for the bars that have not been handled yet
for(int i=prev_calculated;i<rates_total;i++)
{
//--- 0 by default
buff[i]=0;
for(int j=0;j<size;j++)
{
//--- if dates coincide, set as close price
if(time[i]==time_buff[j])
{
//--- set as close price
buff[i]=close[i];
}
}
}
//---
return(rates_total);
}另请参阅
最后更新于