FileWriteStruct

この関数は、バイナリファイルのファイルポインタの現在の位置から、パラメータとして渡される構造体のコンテンツを書き込みます。

uint  FileWriteStruct(
  int          file_handle,       // ファイルハンドル
  const void&  struct_object,    // オブジェクトへのリンク
  int          size=-1           // 書かれるバイト数
  );

パラメータ

file_handle

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

struct_object

[in] 構造体オブジェクトへの参照。構造体は文字列、動的配列 また 仮想関数を含むことは出来ません。

size=-1

[in] 書かれるバイト数サイズが指定されない、または指定されたバイト数が構造のサイズよりも大きい場合は、構造体全体が書かれます。

戻り値

成功の場合、書かれたバイト数。ファイルポインタは、書かれたバイト数によって動きます。

例:

//+------------------------------------------------------------------+
//|                                          Demo_FileWiteStruct.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 script_show_inputs
//--- 端末からデータを受け取るパラメータ
input string         InpSymbolName="EURUSD";           // 通貨ペア
input ENUM_TIMEFRAMES InpSymbolPeriod=PERIOD_H1;       // 時間軸
input datetime       InpDateStart=D'2013.01.01 00:00'; // データコピー開始日
//--- データをファイルに書くパラメータ
input string         InpFileName="EURUSD.txt";         // ファイル名
input string         InpDirectoryName="Data";         // ディレクトリ名
//+------------------------------------------------------------------+
//| ローソク足データを格納する構造体                                          |
//+------------------------------------------------------------------+
struct candlesticks
 {
  double            open; // 始値
  double            close; // 終値
  double            high; // 高値
  double            low;   // 安値
  datetime          date; // 日付
 };
//+------------------------------------------------------------------+
//| スクリプトプログラムを開始する関数                                          |
//+------------------------------------------------------------------+
void OnStart()
 {
  datetime     date_finish=TimeCurrent();
  int          size;
  datetime     time_buff[];
  double       open_buff[];
  double       close_buff[];
  double       high_buff[];
  double       low_buff[];
  candlesticks cand_buff[];
//--- エラー値をリセットする
  ResetLastError();
//--- 範囲内のバーの到着時間を受信
  if(CopyTime(InpSymbolName,InpSymbolPeriod,InpDateStart,date_finish,time_buff)==-1)
    {
    PrintFormat("Failed to copy time values. Error code = %d",GetLastError());
    return;
    }
//--- 範囲内の足の高値を受信
  if(CopyHigh(InpSymbolName,InpSymbolPeriod,InpDateStart,date_finish,high_buff)==-1)
    {
    PrintFormat("Failed to copy values of high prices. Error code = %d",GetLastError());
    return;
    }
//--- 範囲内のバーの安値を受信
  if(CopyLow(InpSymbolName,InpSymbolPeriod,InpDateStart,date_finish,low_buff)==-1)
    {
    PrintFormat("Failed to copy values of low prices. Error code = %d",GetLastError());
    return;
    }
//--- 範囲内のバーの始値を受信
  if(CopyOpen(InpSymbolName,InpSymbolPeriod,InpDateStart,date_finish,open_buff)==-1)
    {
    PrintFormat("Failed to copy values of open prices. Error code = %d",GetLastError());
    return;
    }
//--- 範囲内のバーの終値を受信
  if(CopyClose(InpSymbolName,InpSymbolPeriod,InpDateStart,date_finish,close_buff)==-1)
    {
    PrintFormat("Failed to copy values of close prices. Error code = %d",GetLastError());
    return;
    }
//--- 配列の次元を定義
  size=ArraySize(time_buff);
//--- データを構造体配列に保存
  ArrayResize(cand_buff,size);
  for(int i=0;i<size;i++)
    {
     cand_buff[i].open=open_buff[i];
     cand_buff[i].close=close_buff[i];
     cand_buff[i].high=high_buff[i];
     cand_buff[i].low=low_buff[i];
     cand_buff[i].date=time_buff[i];
    }
 
//--- 構造体配列を書き込むためにファイルを開く(ファイルが存在しない場合は自動的に作成される)
  ResetLastError();
  int file_handle=FileOpen(InpDirectoryName+"//"+InpFileName,FILE_READ|FILE_WRITE|FILE_BIN|FILE_COMMON);
  if(file_handle!=INVALID_HANDLE)
    {
    PrintFormat("%s file is open for writing",InpFileName);
    PrintFormat("File path: %s\\Files\\",TerminalInfoString(TERMINAL_COMMONDATA_PATH));
    //--- バイト数カウンタを準備する
    uint counter=0;
    //--- ループで配列値を書く
    for(int i=0;i<size;i++)
        counter+=FileWriteStruct(file_handle,cand_buff[i]);
    PrintFormat("%d bytes of information is written to %s file",InpFileName,counter);
    PrintFormat("Total number of bytes: %d * %d * %d = %d, %s",size,5,8,size*5*8,size*5*8==counter ? "Correct" : "Error");
    //--- ファイルを閉じる
    FileClose(file_handle);
    PrintFormat("Data is written, %s file is closed",InpFileName);
    }
  else
    PrintFormat("Failed to open %s file, Error code = %d",InpFileName,GetLastError());
 }

参照

構造体とクラス