FileWriteArray
FileWriteArray
此函数将除字符串以外的任何类型的数组写入BIN文件(可以是不包含字符串的结构数组或动态数组)。
uint FileWriteArray(
int file_handle, // File handle
const void& array[], // Array
int start=0, // Start index in the array
int count=WHOLE_ARRAY // Number of elements
);参数
- file_handle
[in] 由FileOpen()返回的文件描述符。
- array[]
[out] 用于记录的数组。
- start=0
[in] 数组中初始索引(第一个记录元素的编号)。
- count=WHOLE_ARRAY
[in] 要写入的项目数量(WHOLE_ARRAY表示从start编号开始直到数组末尾的所有项目)。
返回值
写入的元素数量,或发生错误时返回0。要获取关于错误的信息,请调用GetLastError()函数。
注意
字符串数组可以写入TXT文件中。在这种情况下,字符串会自动用换行符“\r\n”结束。根据文件类型是ANSI还是UNICODE,字符串将被转换为ANSI编码或不予转换。
示例:
//+------------------------------------------------------------------+
//| Demo_FileWriteArray.mq5 |
//| Copyright 2013, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
//--- input parameters
input string InpFileName="data.bin";
input string InpDirectoryName="SomeFolder";
//+------------------------------------------------------------------+
//| Structure for storing price data |
//+------------------------------------------------------------------+
struct prices
{
datetime date; // date
double bid; // bid price
double ask; // ask price
};
//--- global variables
int count=0;
int size=20;
string path=InpDirectoryName+"//"+InpFileName;
prices arr[];
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- allocate memory for the array
ArrayResize(arr,size);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
//--- write the remaining count strings if count<n
WriteData(count);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick()
{
//--- save data to array
arr[count].date=TimeCurrent();
arr[count].bid=SymbolInfoDouble(Symbol(),SYMBOL_BID);
arr[count].ask=SymbolInfoDouble(Symbol(),SYMBOL_ASK);
//--- show current data
Print("Date = ",arr[count].date," Bid = ",arr[count].bid," Ask = ",arr[count].ask);
//--- increase the counter
count++;
//--- if the array is filled, write data to the file and zero it out
if(count==size)
{
WriteData(size);
count=0;
}
}
//+------------------------------------------------------------------+
//| Write n elements of the array to file |
//+------------------------------------------------------------------+
void WriteData(const int n)
{
//--- open the file
ResetLastError();
int handle=FileOpen(path,FILE_READ|FILE_WRITE|FILE_BIN);
if(handle!=INVALID_HANDLE)
{
//--- write array data to the end of the file
FileSeek(handle,0,SEEK_END);
FileWriteArray(handle,arr,0,n);
//--- close the file
FileClose(handle);
}
else
Print("Failed to open the file, error ",GetLastError());
}另请参阅
最后更新于