Datetime fileread error: returning 1970 date instead of actual on file.

 

Hi Guys,

I've got a below standard piece of code for reading from a file with the following format: YYYY.MM.DD HH:MM; integer

What I get returned for MD (date) is 1970.01.01 00:00:00 instead of the actual date on file.

Do you have any solution guys? Thanks!

int Handle, Number, Number2;
string File_Name = "Test.csv", str_date;
datetime MD;

while(FileIsEnding(Handle)==false)  
     {                                 
      str_date =FileReadString(Handle);// Date and time until first break
      Number   =FileReadString(Handle);//  1st trader's performance
      Number2  =FileReadString(Handle);//  2nd trader's performance
      
      if(FileIsEnding(Handle)==true)   // File pointer is at the end
         break;                        // Break
         
      MD =StrToTime(str_date);   // Transformation of data type
    
      }
     
      Alert(Number);
      Alert(Number2);
      Alert(MD);
      
      FileClose( Handle );
 
If this: "YYYY.MM.DD HH:MM; integer" is the string the function returns 0 which is

1970.01.01 00:00:00 (check _LastError!).

The correct string must be (only") "YYYY.MM.DD hh:mm".

 
gooly:
If this: "YYYY.MM.DD HH:MM; integer" is the string the function returns 0 which is

1970.01.01 00:00:00 (check _LastError!).

The correct string must be (only") "YYYY.MM.DD hh:mm".


This is the format of the .csv. Attached below. The file reads: 2015.08.29 10:30;150    

Thanks for your time.

 
int Handle, Number, Number2;
string File_Name = "Test.csv", str_date;
datetime MD;

while(FileIsEnding(Handle)==false)  
     {                                 
      str_date =FileReadString(Handle);// Date and time until first break
      Number   =FileReadString(Handle);//  1st trader's performance
      Number2  =FileReadString(Handle);//  2nd trader's performance
      
      if(FileIsEnding(Handle)==true)   // File pointer is at the end
         break;                        // Break
         
      MD =StrToTime(str_date);   // Transformation of data type
    
      }
     
      Alert(Number);
      Alert(Number2);
      Alert(MD);
      
      FileClose( Handle );
You have to open the file before you can read it
 
GumRai:
You have to open the file before you can read it


Sure, full code below:


int Handle, Number, Number2;
   string File_Name = "test.csv", str_date;
   datetime MD;
   
   Handle = FileOpen(File_Name,FILE_CSV|FILE_READ,";");
   if(Handle<0)                        // Error handling 
     {
      if(GetLastError()==4103)         
         Alert("No file named ",File_Name);
      else                             
         Alert("Error while opening file ",File_Name); //For all other errors.
      Alert("Error");                                   
     }
    
    if(Handle>0)
      {
        Alert("Done ",Handle);
      }
      
  
    while(FileIsEnding(Handle)==false)  
     {                                 
      str_date =FileReadString(Handle);// Date and time until first break
      Number   =FileReadString(Handle);//  1st trader's performance
      Number2  =FileReadString(Handle);//  2nd trader's performance
      
      if(FileIsEnding(Handle)==true)   // File pointer is at the end
         break;                        // Break
         
      MD =StrToTime(str_date);   // Transformation of data type
      
      }
     
      Alert(Number);
      Alert(Number2);
      Alert(MD);
      
      FileClose( Handle );   
 
Vlaser91:


This is the format of the .csv. Attached below. The file reads: 2015.08.29 10:30;150    

Thanks for your time.

Is this what the cell that holds the date has? "2015.08.29 10:30;150"  

When you open the file, you specify ";" as the delimiter.  Could this be trying to read 4 cells when there are only 3 per line? 

 
GumRai:

Is this what the cell that holds the date has? "2015.08.29 10:30;150"  

When you open the file, you specify ";" as the delimiter.  Could this be trying to read 4 cells when there are only 3 per line? 


I just had a look on your suggestion. After placing an Alert below:

str_date =FileReadString(Handle);// Date and time until first break


I can see that the String of date is correctly parsed. I am guessing is the post-processing in this line that causes the issue:

MD =StrToTime(str_date);   // Transformation of data type


The above is supposed to convert the string

str_date

to datetime format and pass it onto MD (already assigned as datetime).

That doesn't work.

 
  1. Don't tell us what you see. Print out the string and the date so we know. Or attach the file like you said you did.
  2. Since you know it's a dt, why not just read it directly? FileReadDatetime - MQL4 Documentation



 
WHRoeder:
  1. Don't tell us what you see. Print out the string and the date so we know. Or attach the file like you said you did.
  2. Since you know it's a dt, why not just read it directly? FileReadDatetime - MQL4 Documentation




So, top entry is the MD string, followed by Number, Number2 and last you can see the String before the first semicolon. This is the initial read.

The file consists of DATE ; integer. For some strange reason although I am attaching the file, it doesn't upload. Below is a screenshot of the file.

Thanks for the interest Guys.

 
  1. The file consists of DATE
    No it doesn't; there is no month 29. As Gooly pointed out. See https://www.mql5.com/en/forum/155397

  2. Since the delimiter is a short, not a string.
    Handle = FileOpen(File_Name,FILE_CSV|FILE_READ,";");
    Try
    Handle = FileOpen(File_Name,FILE_CSV|FILE_READ,';');

 
WHRoeder:
  1. The file consists of DATE
    No it doesn't; there is no month 29. As Gooly pointed out. See https://www.mql5.com/en/forum/155397

  2. Since the delimiter is a short, not a string.
    Try


Thanks a lot WHRoeder, that didn't work but got me thinking. Given that the

str_date

 holds exactly the wanted String, I tried not assigning the

StringToTime(str_date);

to MD but having the following Alert directly:

Alert(StringToTime(str_date));

This did work, with a twist....

Remember that the correct date was: 2015.29.08 10:44

Still unclear the cause.

Reason: