FileReadStruct

이 함수는 파일 포인터의 현재 위치부터 시작하여 이진 파일에서 파라미터로 전달된 구조로 내용을 읽습니다.

uint  FileReadStruct(
   int          file_handle,        // 파일 핸들
   const void&  struct_object,      // 내용을 읽을 대상 구조
   int          size=-1             // 구조 크기(바이트)
   );

Parameters

file_handle

[in] 열린 bin 파일의 파일 설명자.

struct_object

[out]  이 구조의 개체. 구조에는 문자열, 동적 배열 또는 가상 함수가 포함되어서는 안 됩니다.

size=-1

[in]  읽어져야 할 바이트 수. 크기가 지정되지 않았거나 표시된 값이 구조의 크기보다 큰 경우 지정된 구조의 정확한 크기가 사용됩니다.

반환 값

성공하면 함수는 읽은 바이트 수를 반환합니다. 파일 포인터가 동일한 바이트 수만큼 이동합니다.

(FileWriteStruct 함수에 대한 예제를 실행한 후 얻은 파일이 여기에 사용됩니다)

//+------------------------------------------------------------------+
//|                                          Demo_FileReadStruct.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_separate_window
#property indicator_buffers 4
#property indicator_plots   1
//---- plot Label1
#property indicator_label1  "Candles"
#property indicator_type1   DRAW_CANDLES
#property indicator_color1  clrOrange
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
#property indicator_separate_window
//--- 데이터 수신에 대한 매개 변수
input string  InpFileName="EURUSD.txt"// 파일명
input string  InpDirectoryName="Data";  // 디렉토리명
//+------------------------------------------------------------------+
//| 캔들스틱 데이터 저장 구조                           |
//+------------------------------------------------------------------+
struct candlesticks
  {
   double            open;  // 시가
   double            close; // 종가
   double            high;  // 고가
   double            low;   // 저가
   datetime          date;  // 날짜
  };
//--- 지표 버퍼
double       open_buff[];
double       close_buff[];
double       high_buff[];
double       low_buff[];
//--- 글로벌 변수
candlesticks cand_buff[];
int          size=0;
int          ind=0;
//+------------------------------------------------------------------+
//| 커스텀 지표 초기화 기능                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   int default_size=100;
   ArrayResize(cand_buff,default_size);
//--- 파일 열기
   ResetLastError();
   int file_handle=FileOpen(InpDirectoryName+"//"+InpFileName,FILE_READ|FILE_BIN|FILE_COMMON);
   if(file_handle!=INVALID_HANDLE)
     {
      PrintFormat("%s 파일을 읽을 수 있습니다",InpFileName);
      PrintFormat("파일 경로: %s\\Files\\",TerminalInfoString(TERMINAL_COMMONDATA_PATH));
      //--- 파일에서 데이터 읽기
      while(!FileIsEnding(file_handle))
        {
         //--- 배열에 데이터 작성
         FileReadStruct(file_handle,cand_buff[size]);
         size++;
         //--- 배열이 오버플로 되었는지 확인
         if(size==default_size)
           {
            //--- 배열 크기 증가
            default_size+=100;
            ArrayResize(cand_buff,default_size);
           }
        }
      //--- 파일 닫기
      FileClose(file_handle);
      PrintFormat("데이터를 읽습니다, %s 파일이 닫힙니다",InpFileName);
     }
   else
     {
      PrintFormat("%s 파일 열기 실패, 에러 코드 = %d",InpFileName,GetLastError());
      return(INIT_FAILED);
     }
//--- 지표 버퍼 연결
   SetIndexBuffer(0,open_buff,INDICATOR_DATA);
   SetIndexBuffer(1,high_buff,INDICATOR_DATA);
   SetIndexBuffer(2,low_buff,INDICATOR_DATA);
   SetIndexBuffer(3,close_buff,INDICATOR_DATA);
//--- 빈(empty) 값
   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);
//--- 아직 처리되지 않은 캔들스틱의 루프
   for(int i=prev_calculated;i<rates_total;i++)
     {
      //--- 디폴트로 0
      open_buff[i]=0;
      close_buff[i]=0;
      high_buff[i]=0;
      low_buff[i]=0;
      //--- 데이터가 아직 있는지 확인
      if(ind<size)
        {
         for(int j=ind;j<size;j++)
           {
            //--- 날짜가 일치하면 파일의 값이 사용됩니다
            if(time[i]==cand_buff[j].date)
              {
               open_buff[i]=cand_buff[j].open;
               close_buff[i]=cand_buff[j].close;
               high_buff[i]=cand_buff[j].high;
               low_buff[i]=cand_buff[j].low;
               //--- 카운터 증가
               ind=j+1;
               break;
              }
           }
        }
     }
//--- 다음 호출을 위한 prev_calculated의 반환 값
   return(rates_total);
  }

더 보기

Structures and classes, FileWriteStruct