FileReadInteger

この関数は、ファイルポインタの現在位置から、バイト単位で指定された長差に従って int、short または char 値を読み込みます。

int  FileReadInteger(
  int  file_handle,        // ファイルハンドル
  int  size=INT_VALUE      // バイト単位での整数のサイズ
  );

パラメータ

file_handle

[in] FileOpen() から戻されたファイル記述子

size=INT_VALUE

[in]  読まれるべきバイト数(4 以下)。関数が char、short または int 型の値を読めるように、CHAR_VALUE=1、SHORT_VALUE=2 t及び INT_VALUE=4 と対応する定数が用意されています。

戻り値

int 型の値この関数の結果は、読まれるために必要なデータの型であるターゲットの型に明確的にキャストする必要があります。int 型の値が返されるので、任意の整数値に変換することは容易です。ファイルポインタは、読み込まれたバイト数によってシフトされます。

注意事項

4 バイト未満が読み取られた場合、結果は常に正です。1 または 2 バイトが読み取られた場合、数値の符号は、char( 1 バイト)又は short( 2 バイト)に明示的にキャストすることによって決定出来ます。基になる型が存在しないため、3バイトの数の記号を取得することは簡単ではありません。

FileWriteInteger 関数の例の実行によって取得されたファイルが使用されます)

//+------------------------------------------------------------------+
//|                                         Demo_FileReadInteger.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"
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots   1
//---- Label1 をプロットする
#property indicator_label1 "Trends"
#property indicator_type1   DRAW_SECTION
#property indicator_color1 clrRed
#property indicator_style1 STYLE_SOLID
#property indicator_width1  1
//--- データ読み込みのパラメータ
input string InpFileName="Trend.bin"; // ファイル名
input string InpDirectoryName="Data"; // ディレクトリ名
//--- グローバル変数
int      ind=0;
int      size=0;
datetime time_buff[];
//--- 指標バッファ
double   buff[];
//+------------------------------------------------------------------+
//| カスタム指標を初期化する関数                                            |
//+------------------------------------------------------------------+
int OnInit()
 {
  int def_size=100;
//--- 配列へのメモリ追加
  ArrayResize(time_buff,def_size);
//--- ファイルを開く
  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));
    //--- 追加の変数
    int    arr_size;
    uchar  arr[];
    //--- ファイルからデータを読む
    while(!FileIsEnding(file_handle))
       {
        //--- 時間を書くのに使用されるシンボルの数を見つける
        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);
        //--- 時間値を格納する
        time_buff[size]=StringToTime(CharArrayToString(arr));
        size++;
        //--- オーバーフローしている場合は、配列のサイズを増やす
        if(size==def_size)
          {
           def_size+=100;
          ArrayResize(time_buff,def_size);
          }
       }
    //--- ファイルを閉じる
    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);
    }
//--- 配列と指標バッファを関連付ける
  SetIndexBuffer(0,buff,INDICATOR_DATA);
//---- チャートでは表示されない指標値を設定する
  PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0);
//---
  return(INIT_SUCCEEDED);
 }
//+------------------------------------------------------------------+
//| カスタム指標の反復関数                                                |
//+------------------------------------------------------------------+
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,false);
  ArraySetAsSeries(close,false);
//--- まだ処理されてないバーのループ
  for(int i=prev_calculated;i<rates_total;i++)
    {
    //--- デフォルトでは 0
     buff[i]=0;
    //--- まだデータがあるかをチェック
    if(ind<size)
       {
        for(int j=ind;j<size;j++)
          {
          //--- 日付が同じならファイルの値を使用する
          if(time[i]==time_buff[j])
             {
              //--- 価格を受け取る
              buff[i]=close[i];
              //--- カウンタを増加する
              ind=j+1;
              break;
             }
          }
       }
    }
//--- 次の呼び出しのために prev_calculated の値を返す
  return(rates_total);
 }

参照

IntegerToStringStringToInteger整数型FileWriteInteger