Read from CSV - where is error?

 

Hi,

sorry my stupid question but.... I don't see error. I have csv file:

1,2

3,4

5,6

and here is code for reading data to array arMM[][], but not works correct :-(

void Read_MM_from_File()
{ int rows;
  double arMM[][2];
  string File_MM = "test1.csv";
  int handle=FileOpen(File_MM,FILE_CSV|FILE_READ,",");// File opening
  if(handle<0)                        // File opening fails
  {
    if(GetLastError()==4103)         // If the file does not exist,..
      Alert("No file named ",File_MM);//.. inform trader
    else                             // If any other error occurs..
      Alert("Error while opening file ",File_MM);//..this message
  }
  //--------------------------------------------------------------- 4 --
   
   while((FileIsEnding(handle)==false) /*&& rows<10*/)  // While the file pointer..
   {                                   // ..is not at the end of the file
      rows=ArraySize(arMM);
      ArrayResize(arMM,rows+1);
      arMM[rows,0] = FileReadNumber(handle);// Date and time of the event (date)
      arMM[rows,1]   =FileReadNumber(handle);
      Alert(rows, " risk=",arMM[rows,0], " tp=",arMM[rows,1]);
   }
   FileClose(handle);
}
 
Third argument to FileOpen is an int
 
endy5:

Hi,

sorry my stupid question but.... I don't see error. I have csv file:

1,2

3,4

5,6

and here is code for reading data to array arMM[][], but not works correct :-(

Just incase you didn't get what WHRoeder meant . . . in this:

int handle=FileOpen(File_MM,FILE_CSV|FILE_READ,  ","  );// File opening

should be . . .

int handle=FileOpen(File_MM,FILE_CSV|FILE_READ,   ','  );// File opening
 
WHRoeder:
Third argument to FileOpen is an int
RaptorUK:

Just incase you didn't get what WHRoeder meant . . . in this:

int handle=FileOpen(File_MM,FILE_CSV|FILE_READ,  ","  );// File opening

should be . . .

int handle=FileOpen(File_MM,FILE_CSV|FILE_READ,   ','  );// File opening

I think I disagree. I can read the file just fine...with either "," or ','. I think the problem lies with the FileIsEnding(handle) in the While loop. It seems (at least in the small tests that I've ran) that the script hangs--apparently because FileIsEnding(handle) seems not to return true.
 
Thirteen:
I think I disagree. I can read the file just fine...with either "," or ','. I think the problem lies with the FileIsEnding(handle) in the While loop. It seems (at least in the small tests that I've ran) that the script hangs--apparently because FileIsEnding(handle) seems not to return true.


Could be, I've had problems trying to use FileIsEnding() it seems to say FileHasEnded rather than IsEnded . . . and returns false even if the file point is at the end of the file unless the file is then subsequently read . . . as far as I recall.
 

If the CSV file contains the following data:

1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,

then the following modified code:

void Read_MM_from_File()
{ int rows;
  double arMM[][2];
  string File_MM = "test1.csv";
  int handle=FileOpen(File_MM,FILE_CSV|FILE_READ,",");// File opening
  if(handle<0)                        // File opening fails
  {
    if(GetLastError()==4103)         // If the file does not exist,..
      Alert("No file named ",File_MM);//.. inform trader
    else                             // If any other error occurs..
      Alert("Error while opening file ",File_MM);//..this message
  }
  //--------------------------------------------------------------- 4 --
  
   while (!FileIsEnding(handle)) {
      rows = ArrayRange(arMM, 0);
      ArrayResize(arMM,rows+1);
      arMM[rows,0] = FileReadNumber(handle);// Date and time of the event (date)
      arMM[rows,1] = FileReadNumber(handle);
      Print (rows, " risk=",arMM[rows,0], " tp=",arMM[rows,1]);
   }
   FileClose(handle);
}

produces the following output to the journal/log:

19:20:04 TestScript EURUSD,H1: loaded successfully

19:20:04 TestScript EURUSD,H1: 0 risk=1 tp=2

19:20:04 TestScript EURUSD,H1: 1 risk=3 tp=4

19:20:04 TestScript EURUSD,H1: 2 risk=5 tp=6

19:20:04 TestScript EURUSD,H1: 3 risk=7 tp=8

19:20:04 TestScript EURUSD,H1: uninit reason 0

19:20:04 TestScript EURUSD,H1: removed

 

Thank you everyone! Thirteen - Thanks for code!

But my CSV file will be XYZ rows, for file with only one row is code correct, for more rows no :-(

A] If my file is:

row1: 1.0,2.0

row2: 3.0,4.0

row3: 5.0,6.0 +< ENTER>

is output from log: 09:15:17

09:15:17 _info_swap AUDUSD..,M15: 0 risk=1 tp=2

09:15:17 _info_swap AUDUSD..,M15: 1 risk=3 tp=4

09:15:17 _info_swap AUDUSD..,M15: 2 risk=5 tp=6

09:15:17 _info_swap AUDUSD..,M15: 3 risk=0 tp=0

______________________________________________________

B] If my file is:

row1: 1.0,2.0

row2: 3.0,4.0

row3: 5.0,6.0 <NO ENTER>

is output from log: 09:15:17

09:16:47 _info_swap AUDUSD..,M15: 0 risk=1 tp=2

09:16:47 _info_swap AUDUSD..,M15: 1 risk=3 tp=4

end script running all time .... no detect end of file ???

Reason: