Download MetaTrader 5
To add comments, please log in or register
Publish your article and thousands of people all over the world will read it!
user_123
172
user_123 2014.03.25 14:09 

Dear Community,

how can I get the value of the last line in the CSV file? I have tried to write this code, but it returns me not the correct value. I have found some older post in the forum, but they don’t help me.

Thank you very much!


   int file_handle2=FileOpen(InpDirectoryName+"//"+FileName,FILE_READ|FILE_WRITE|FILE_CSV);
   if(file_handle2!=INVALID_HANDLE)
     {
      FileSeek(file_handle2,0,SEEK_END);
      Print("Datetime: " + TimeToStr(FileReadDatetime(file_handle2),TIME_DATE|TIME_MINUTES));
      //Print("Datetime: " +FileReadString(file_handle2,10));
      //--- close the file
      FileClose(file_handle2);
     }
whroeder1
13596
whroeder1 2014.03.25 15:08  
  1. Your seek it to the end of the file not to the last line. Past the last line.
  2. You can NOT seek, because the file has variable length lines.
  3. Remember the last line read, read the next line. If it is empty, you've reached the end of file. The previous line WAS the last one.
user_123
172
user_123 2014.03.25 16:21  

Dear WHRoeder,

After a while loop I'm a line too far. How can I keep the line before? I thought maybe will this work, but it does not

Thank you!


      while(!FileIsEnding(file_handle2)){
            FileReadDatetime(file_handle2);
      }
      Print("Datetime: " + TimeToStr(FileReadDatetime(file_handle2),TIME_DATE|TIME_MINUTES));
      FileClose(file_handle2);
user_123
172
user_123 2014.03.25 16:54  

This does not work too and I do not know why.. Have I a error in reasoning?

   int file_handle2=FileOpen(InpDirectoryName+"//"+FileName,FILE_READ|FILE_WRITE|FILE_CSV);
   if(file_handle2!=INVALID_HANDLE){
      datetime MyTime=0;
      while(!FileIsEnding(file_handle2)){
         MyTime = FileReadDatetime(file_handle2); 
      }
      Print("Datetime: " + TimeToStr(MyTime,TIME_DATE|TIME_MINUTES));
      FileClose(file_handle2);
   }
Alain Verleyen
Moderator
28522
Alain Verleyen 2014.03.25 17:05  
user_123:

This does not work too and I do not know why.. Have I a error in reasoning?

Maybe doing what WHRoeder wrote :

   int file_handle2=FileOpen(InpDirectoryName+"//"+FileName,FILE_READ|FILE_WRITE|FILE_CSV);
   if(file_handle2!=INVALID_HANDLE){
      datetime MyTime=0,Previous=0;
      while(!FileIsEnding(file_handle2)){
         Previous = MyTime;
         MyTime = FileReadDatetime(file_handle2); 
      }
      Print("Datetime: " + TimeToStr(Previous,TIME_DATE|TIME_MINUTES));
      FileClose(file_handle2);
   }  
user_123
172
user_123 2014.03.25 17:26  

Thank you WHRoeder and angevoyageur,

Sorry it was my fault,

I have forgotten to write that my csv-file has the following format. And I want to read the last date and the last price in this example 03.03.2014 17:44 and 1.28333. I saw now that the problem is the second column, but i do not know how I can solve the problem.

I would be very grateful if you could help me

03.03.2014 17:42
1.25666
03.03.2014 17:431.25777
03.03.2014 17:441.28333


(I have tried it, if my csv-file have only the first column, than the upper solution does work)

I thought maybe this will work, but it does not:

   int file_handle2=FileOpen(InpDirectoryName+"//"+FileName,FILE_READ|FILE_WRITE|FILE_CSV);
   if(file_handle2!=INVALID_HANDLE){
      datetime MyTime=0,Previous=0;
      double price = 0;
       while(!FileIsEnding(file_handle2)){
         MyTime = FileReadDatetime(file_handle2);
         price = FileReadDouble(file_handle2);
      }
      Print("Datetime: " + TimeToStr(MyTime,TIME_DATE|TIME_MINUTES));
      Print("Price: " + DoubleToStr(price));
      FileClose(file_handle2);
   }
qjol
3243
qjol 2014.03.25 19:35  

why not use the same method angevoyageur have written here (use just a little of your head)

   int file_handle2=FileOpen(InpDirectoryName+"//"+FileName,FILE_READ|FILE_WRITE|FILE_CSV);
   if(file_handle2!=INVALID_HANDLE){
   datetime MyTime=0,PreviousTime=0;
   double price = 0, PreviousPrice=0;
     while(!FileIsEnding(file_handle2)){
         PreviousTime = MyTime;
         PreviousPrice = price;
         MyTime = FileReadDatetime(file_handle2);
         price = FileReadDouble(file_handle2);
      }
      Print("Datetime: " + TimeToStr(PreviousTime,TIME_DATE|TIME_MINUTES));
      Print("Price: " + DoubleToStr(PreviousPrice));
      FileClose(file_handle2);
   }
user_123
172
user_123 2014.03.25 20:35  
qjol:

why not use the same method angevoyageur have written here (use just a little of your head)



Unfortunately, your suggestion does not work, because you can not use the FileReadDouble function with the csv-File
qjol
3243
qjol 2014.03.25 21:06  
then use FileReadString and convert it using StringToTime and StringToDouble
Carl Schreiber
5934
Carl Schreiber 2014.03.25 21:21  

After struggling with faile save csv-read I do with a csv-file:

        handle   = FileOpen(fName, FILE_SHARE_READ | FILE_READ | FILE_BIN);
        szFile   = (int)FileSize(handle);
        strFile  = FileReadString(handle, szFile);
        allLines = StringSplit(strFile,'\n',arrFile);
        FileClose(handle);
        int LineNo = 0;
        while (LineNo < allLines) { 
                maxTab  = StringSplit(arrFile[LineNo],'\t',arrTab);
...
/
To add comments, please log in or register