FileReadInteger

이 함수는 파일 포인터의 현재 위치에서 바이트 단위로 지정된 길이에 따라 int, short 또는 char 값을 읽습니다.

int  FileReadInteger(
   int  file_handle,        // 파일 핸들
   int  size=INT_VALUE      // 정수의 바이트 단위의 사이즈
   );

Parameters

file_handle

[in] FileOpen()에서 반환된 파일 설명자.

size=INT_VALUE

[in]  읽어야 하는 바이트 수 (최대 4). 해당하는 상수가 제공됩니다: CHAR_VALUE = 1, SHORT_VALUE = 2 및 INT_VALUE = 4, 따라서 함수는 char, short 또는 int 유형의 전체 값을 읽을 수 있습니다.

반환 값

int 유형의 값 이 기능의 결과는 대상 유형, 즉 읽어야 하는 데이터 유형에 명시적으로 캐스트해야 합니다. int 유형의 값이 반환되므로 모든 정수 값으로 쉽게 변환할 수 있습니다. 파일 포인터가 읽은 바이트 수에 따라 이동됩니다.

참고

읽기가 4바이트 미만일 경우 수신된 결과는 항상 양수입니다. 1 또는 2 바이트를 읽을 경우 숫자 기호는 문자(1바이트) 또는 짧은 문자(2바이트)로 명시적으로 캐스팅하여 확인할 수 있습니다. 해당하는 underlying 타입이 없으므로 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
//---- plot 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"// file name
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 파일을 읽을 수 있습니다",InpFileName);
      PrintFormat("파일 경로: %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("데이터를 읽습니다, %s 파일이 닫힙니다",InpFileName);
     }
   else
     {
      PrintFormat("%s 파일 열기 실패, 에러 코드 = %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);
  }

더 보기

IntegerToString, StringToInteger, Integer types, FileWriteInteger