TEXT FILE: I can only read the first line! Why?? Thanks!

 

Hi everyone!

I want to read this text file :

237     DC      8.9     12.5
217     DC      7.5     10.6
316     DJ      5.9     8.4
257     DN      10.0    12.7
355     DU      4.6     6.6
190     DC      8.6     12.1
267     DJ      7.2     9.7
222     FJ      8.5     11.8

Each column is separated by a tab (hexa 0x9).

Each line ends by a CRLF (hexa 0xD 0xA)

For some reason, my program loops forever:

237.00000000 DC 8.90000000 12.50000000
237.00000000 DC 8.90000000 12.50000000
237.00000000 DC 8.90000000 12.50000000
237.00000000 DC 8.90000000 12.50000000
237.00000000 DC 8.90000000 12.50000000
....

Any idea why??   THANKS!

int OnInit()
  {
   string file=TerminalInfoString(TERMINAL_DATA_PATH)+"\\MQL4\\Files\\test.csv";
   string str="";

   ResetLastError();
   int file_handle=FileOpen("test.csv",FILE_READ|FILE_CSV|FILE_ANSI,'\t');
   if(file_handle!=INVALID_HANDLE)
     {
      //--- read data from file
      while(!FileIsEnding(file_handle))
        {
         //--- read till the end of the string or of the file is reached
         while(!FileIsLineEnding(file_handle))
           {
            str+=DoubleToStr(FileReadNumber(file_handle));
            str+=" "+FileReadString(file_handle);
            str+=" "+DoubleToStr(FileReadNumber(file_handle));
            str+=" "+DoubleToStr(FileReadNumber(file_handle));
           }
         Print(str);
      //   str="";
        }
      //--- close the file
      FileClose(file_handle);
      PrintFormat("Data is read, %s file is closed",file);
     }
   else
      PrintFormat("Failed to open %s file, Error code = %d",file,GetLastError());
   return(INIT_SUCCEEDED);
  }
 
  1. You know the format, no need for the line is ending loop. That is in case you have a variable width file and need to check before the next read. After you read the last column, you are on the next line and the function returns false, you loop forever, never checking if file is ending.

  2. You are appending reading all lines into str.
 
A BIG THANK YOU!!!!!!!!!!!!! Have a nice day!
Reason: