MQL4参考 文件函数 FileIsLineEnding

FileIsLineEnding

Defines the line end in a text file in the process of reading.

bool  FileIsLineEnding(
   int  file_handle      // File handle
   );

Parameters

file_handle

[in]  File descriptor returned by FileOpen().

Return Value

Returns true if in the process of reading txt or csv-file reached the end of the line (the characters CR-LF).

Example (the file obtained during the execution of an example for FileWriteString() function is used here)

//+------------------------------------------------------------------+
//|                                        Demo_FileIsLineEnding.mq5 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                              https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
//--- parameters for data reading
input string InpFileName="RSI.csv";   // file name
input string InpDirectoryName="Data"// directory name
//--- overbought variables
int      ovb_size=0;
datetime ovb_time[];
//--- oversold variables
int      ovs_size=0;
datetime ovs_time[];
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- open the file
   ResetLastError();
   int file_handle=FileOpen(InpDirectoryName+"//"+InpFileName,FILE_READ|FILE_CSV|FILE_ANSI,"\t");
   if(file_handle!=INVALID_HANDLE)
     {
      PrintFormat("%s file is available for reading",InpFileName);
      PrintFormat("File path: %s\\Files\\",TerminalInfoString(TERMINAL_DATA_PATH));
      double value;
      //--- read data from file
      while(!FileIsEnding(file_handle))
        {
         //--- read the first value in the string
         value=FileReadNumber(file_handle);
         //--- read to different arrays according to the function result
         if(value>=70)
            ReadData(file_handle,ovb_time,ovb_size);
         else
            ReadData(file_handle,ovs_time,ovs_size);
        }
      //--- close the file
      FileClose(file_handle);
      PrintFormat("Data is read, %s file is closed",InpFileName);
      //--- print data
      PrintFormat("Overbought=%d",ovb_size);
      for(int i=0; i<ovb_size; i++) Print(i," time=",TimeToString(ovb_time[i]));
      PrintFormat("Oversold=%d",ovs_size);
      for(int i=0; i<ovs_size; i++) Print(i," time=",TimeToString(ovs_time[i]));
     }
   else
     {
      PrintFormat("Failed to open %s file, Error code = %d",InpFileName,GetLastError());
      return;
     }
//---
  }
//+------------------------------------------------------------------+
//| Read the file's string data                                      |
//+------------------------------------------------------------------+
void ReadData(const int file_handle,datetime &arr[],int &size)
  {
   bool flag=false;
   string str="";
//--- read till the end of the string or of the file is reached
   while(!FileIsLineEnding(file_handle) && !FileIsEnding(file_handle))
     {
      //--- shift the position by reading the number
      if(flag)
         FileReadNumber(file_handle);
      size++;
      //--- increase the array size if necessary
      if(size>ArraySize(arr)) ArrayResize(arr,size,100);
      //--- read date
      arr[size-1]=FileReadDatetime(file_handle);
      //--- add to string
      str+=" "+TimeToString(arr[size-1]);
      //--- slip past the first iteration
      flag=true;
     }
   Print(str);
  }

See also

FileWriteString()