跳至内容

FileReadArray

FileReadArray

从BIN类型的文件中读取数组,数组的类型可以是除字符串以外的任何类型(可以是结构数组,不包含字符串,以及动态数组)。

uint  FileReadArray(
   int    file_handle,               // File handle
   void&  array[],                   // Array to record
   int    start=0,                   // start array position to write
   int    count=WHOLE_ARRAY          // count to read
   );

参数

file_handle

[in] 由FileOpen()返回的文件描述符。

array[]

[out] 数据将被加载到的数组。

start=0

[in] 从数组中读取的起始位置。

count=WHOLE_ARRAY

[in] 要读取的元素数量。默认情况下,读取整个数组(count=WHOLE_ARRAY).

返回值

读取到的元素数量,或发生错误时返回0。要获取关于错误的信息,调用GetLastError()函数。默认情况下,将读取count=WHOLE_ARRAY个元素。

注意

字符串数组只能从TXT类型的文件中读取。如果需要,函数会尝试增加数组的大小。

示例(执行FileWriteArray()函数后获得的文件在此处使用)

//--- display the window of input parameters when launching the script
#property script_show_inputs
//--- 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
  };
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- structure array
   prices arr[];
//--- file path
   string path=InpDirectoryName+"//"+InpFileName;
//--- open the file
   ResetLastError();
   int file_handle=FileOpen(path,FILE_READ|FILE_BIN);
   if(file_handle!=INVALID_HANDLE)
     {
      //--- read all data from the file to the array
      FileReadArray(file_handle,arr);
      //--- receive the array size
      int size=ArraySize(arr);
      //--- print data from the array
      for(int i=0;i<size;i++)
         Print("Date = ",arr[i].date," Bid = ",arr[i].bid," Ask = ",arr[i].ask);
      Print("Total data = ",size);
      //--- close the file
      FileClose(file_handle);
     }
   else
      Print("File open failed, error ",GetLastError());
  }

另请参阅

变量FileWriteArray().

最后更新于