【问题标题】:struct to array to File on mql4mql4 上的结构数组到文件
【发布时间】:2015-06-17 15:46:03
【问题描述】:

以下MQL4代码出现错误:

#property version   "1.00"
#property strict

struct prices
{
    datetime          date; // date
    double            bid;  // bid price
    double            ask;  // ask price
};

prices arr[];
string path ;
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
   //---
    path = "script.log";
   //--- save data to array
    arr[0].date = TimeCurrent();
    arr[0].bid  = SymbolInfoDouble(Symbol(),SYMBOL_BID);
    arr[0].ask  = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
   //--- show current data
   Print("Date = ",arr[0].date," Bid = ",arr[0].bid," Ask = ",arr[0].ask);
   //--- increase the counter
   //--- if the array is filled, write data to the file and zero it out
   WriteData(1);
}
//+------------------------------------------------------------------+
//| 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|FILE_COMMON);
    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());
}
//+------------------------------------------------------------------+

错误是:

2015.06.17 15:39:25.561 array out of range in 'Struct2Array.mq4' (28,8)

第 28 行如下:arr[0].date = TimeCurrent();

知道哪里错了吗?

提前致谢 /库尔。

【问题讨论】:

  • 好吧,基于索引 0 超出范围的事实,我不得不说 arr 的大小为 0。您可能需要初始化它。
  • 通过在影响元素到数组之前添加 ArrayResize 来解决问题。必须进行数组初始化。谢谢

标签: arrays struct mql4


【解决方案1】:

您需要执行以下任一操作:

  1. 在声明时固定数组大小。

    价格 arr[0];

  2. 或者,在 OnStart() 中,调整它的大小:

    ArrayResize(arr, 0)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多