Mql5 Mql4 FileOpen diference ??

 

Hello!

I have below code:

   if(printHistory == true)
     {
      handle = FileOpen("Ticks_" + Symbol() + filename + ".csv", FILE_CSV | FILE_READ | FILE_WRITE,',');
      if(handle < 0)
        {
         Print("Could Not Create/Open File " + IntegerToString(GetLastError()));
         FileClose(handle);
        }
        
        Print("handle=",handle);
      if(handle == 1)
        {
         Print("File Opened Successfully");
         FileSeek(handle,0,SEEK_SET);
         Print("string=",FileReadString(handle));
         FileSeek(handle,0,SEEK_SET);
         if(!FileIsEnding(handle))
            readData(); //Function to Read data line by line from file starting from the earliest record
        }
     }

   if(recordTicks == true && handle == 0)
     {
      handle = FileOpen("Ticks_" + Symbol() + filename + ".csv", FILE_CSV | FILE_READ | FILE_WRITE,',');
      if(handle < 0)
        {
         Print("Could Not Create/Open File " + IntegerToString(GetLastError()));
         FileClose(handle);
        }
      if(handle == 1)
         Print("File Opened Successfully");
      FileSeek(handle, 0, SEEK_END);
     }

in Mql4 if the file is not exist it will open the file, then at the readData() function it will  have nothing to read, and it will move on to the next line.

In Mql5 if the file is not exist it will open the file and then at the readData() function it will read something. Like, first to read should be time, in Mql4 if there is no time recorded in the file, it will not read anything, but in Mql5 it will read 1970.01.01  Why? Also another question, is there any time when the handle can be zero?

 
Anyone?  Thanks ! 
 

About the handle being zero:

Yes. Handle could be zero when it cannot create the file due to some reasons(like lack of permission to create the file on disk).

In case you choose to open file in FILE_READ mode(and not FILE_WRITE) handle is zero if the file does not exist.

 
Daniel Cioca:

Hello!

I have below code:

in Mql4 if the file is not exist it will open the file, then at the readData() function it will  have nothing to read, and it will move on to the next line.

In Mql5 if the file is not exist it will open the file and then at the readData() function it will read something. Like, first to read should be time, in Mql4 if there is no time recorded in the file, it will not read anything, but in Mql5 it will read 1970.01.01  Why? Also another question, is there any time when the handle can be zero?

"If there is no time recorded" means the cell is empty or there are less cells ? (csv columns)

 
Daniel Cioca #:
Anyone?  Thanks ! 
A value of 0 can be a valid handle. An invalid handle can be compared to INVALID_HANDLE constant expression, which evaluates to -1.

A time value of 1970.01.01 00:00:00 is equal to NULL, or 0, or zero.

It is the representation of 0 seconds since beginning of Unix time.

The representation of "nothing" as you would expect, is not a possible value, as you can only represent numbers, and here 0 is chosen for an "empty" value.

To be able to distinguish between "nothing" and zero, you need to approach the reading of the file differently. One option would be to read a string and check it's length.
 
Lorentzos Roussos #:

"If there is no time recorded" means the cell is empty or there are less cells ? (csv columns)

Yes, cell is empty . 
 
Dominik Egert #:
A value of 0 can be a valid handle. An invalid handle can be compared to INVALID_HANDLE constant expression, which evaluates to -1.

A time value of 1970.01.01 00:00:00 is equal to NULL, or 0, or zero.

It is the representation of 0 seconds since beginning of Unix time.

The representation of "nothing" as you would expect, is not a possible value, as you can only represent numbers, and here 0 is chosen for an "empty" value.

To be able to distinguish between "nothing" and zero, you need to approach the reading of the file differently. One option would be to read a string and check it's length.
Thank you