Invalid Handle

 

I do not understand what I am doing wrong here. I am getting Invalid Handle messages for writing the file as well as flushing and closing. See the code and the log. Also, why does it print the correct data only for the first loop. HELLLPPPP!!!!!

int    RestartDetect = 1, AnalHandle = 0, i = 0, j = 0, Row = 3, Column = 5;
double MyArray[1,5];

int start()
 {
  if (RestartDetect > 0)
   {
    RestartDetect = 0;
    if (!AnalHandle == FileOpen("AnalArray.dat", FILE_BIN|FILE_READ))
     {
      Print("Not Else");
      for (i = 0; i < Row; i++)
       {
        for (j = 0; j < Column; j++)
         {
          MyArray[i,j] = j+1;
          Print(MyArray[i,j]);
         }
       }
      for (i = 0; i < Row; i++)
       {
        for (j = 0; j < Column; j++)
         {
          FileWriteArray(AnalHandle, MyArray, i, Column);
         }
       }
      FileFlush(AnalHandle);
      FileClose(AnalHandle); 
     }
    else
     {
      Print("else");
      //read back the whole of MyArray
      if (!AnalHandle == FileOpen("AnalArray.dat", FILE_BIN|FILE_READ))
       {
        Print("No Array File To Read");
        return(0);
       }
      for (i = 0; i < Row; i++)
       {
        for (j = 0; j < Column; j++)
         {
          FileReadArray(AnalHandle, MyArray, i, Column);
         }
       }
      FileFlush(AnalHandle);
      FileClose(AnalHandle);
     } 
   }
 }


LOG shows
09:03:05 Test491013c GBPUSDm,M1: loaded successfully
09:03:18 Test491013c GBPUSDm,M1: Not Else
09:03:18 Test491013c GBPUSDm,M1: 1
09:03:18 Test491013c GBPUSDm,M1: 2
09:03:18 Test491013c GBPUSDm,M1: 3
09:03:18 Test491013c GBPUSDm,M1: 4
09:03:18 Test491013c GBPUSDm,M1: 5
09:03:18 Test491013c GBPUSDm,M1: 0
09:03:18 Test491013c GBPUSDm,M1: 0
09:03:18 Test491013c GBPUSDm,M1: 0
09:03:18 Test491013c GBPUSDm,M1: 0
09:03:18 Test491013c GBPUSDm,M1: 0
09:03:18 Test491013c GBPUSDm,M1: 0
09:03:18 Test491013c GBPUSDm,M1: 0
09:03:18 Test491013c GBPUSDm,M1: 0
09:03:18 Test491013c GBPUSDm,M1: 0
09:03:18 Test491013c GBPUSDm,M1: 0
09:03:18 Test491013c: invalid handle 0 in FileWriteArray
09:03:18 Test491013c: invalid handle 0 in FileWriteArray
09:03:18 Test491013c: invalid handle 0 in FileWriteArray
09:03:18 Test491013c: invalid handle 0 in FileWriteArray
09:03:18 Test491013c: invalid handle 0 in FileWriteArray
09:03:18 Test491013c: invalid handle 0 in FileWriteArray
09:03:18 Test491013c: invalid handle 0 in FileWriteArray
09:03:18 Test491013c: invalid handle 0 in FileWriteArray
09:03:18 Test491013c: invalid handle 0 in FileWriteArray
09:03:18 Test491013c: invalid handle 0 in FileWriteArray
09:03:18 Test491013c: invalid handle 0 in FileWriteArray
09:03:18 Test491013c: invalid handle 0 in FileWriteArray
09:03:18 Test491013c: invalid handle 0 in FileWriteArray
09:03:18 Test491013c: invalid handle 0 in FileWriteArray
09:03:18 Test491013c: invalid handle 0 in FileWriteArray
09:03:18 Test491013c: invalid handle 0 in FileFlush
09:03:18 Test491013c: invalid handle 0 in FileClose
 
Just noticed that code says FileWriteArray(AnalHandle, MyArray, i, Column); and the same for reading. However, it makes no difference at all!!!
 
BigAl:Just noticed that code says FileWriteArray(AnalHandle, MyArray, i, Column); and the same for reading. However, it makes no difference at all!!!

Whats this line suppose to be doing?

if (!AnalHandle == FileOpen("AnalArray.dat", FILE_BIN|FILE_READ))
 
ubzen:

Whats this line suppose to be doing?


It is a test to see if the file already exists. If it does not exist then the code should then build the file. If it does exist it should then read the file and put it back into MyArray. Basically if the EA looses the connection the code should recognise the loss and rebuild the array from the file.
 
BigAl:

I do not understand what I am doing wrong here. I am getting Invalid Handle messages for writing the file as well as flushing and closing. See the code and the log. Also, why does it print the correct data only for the first loop. HELLLPPPP!!!!!

How can you write to a file that you have opened just with read access ?

    if (!AnalHandle == FileOpen("AnalArray.dat",  FILE_BIN|FILE_READ ) )  // <---- Binary file opened for reading . . .
    .
    .
    .
      for (i = 0; i < Row; i++)
       {
        for (j = 0; j < Column; j++)
         {
          FileWriteArray(AnalHandle, MyArray, i, Column);  // <----  writing to file

Please read the documentation: FileOpen()

 
BigAl: It is a test to see if the file already exists. If it does not exist then the code should then build the file. If it does exist it should then read the file and put it back into MyArray. Basically if the EA looses the connection the code should recognise the loss and rebuild the array from the file.

No It doesn't. Where do you assign AnalHandle to the FileHandle before using it in

FileWriteArray(AnalHandle, MyArray, i, Column);
????
 
ubzen:

No It doesn't. Where do you assign AnalHandle to the FileHandle before using it in

????


I guess here :)

int    RestartDetect = 1, AnalHandle = 0, i = 0, j = 0, Row = 3, Column = 5;
 
  1. Your code
        if (!AnalHandle == FileOpen("AnalArray.dat", FILE_BIN|FILE_READ))
    AnalHandle is zero (first time, everytime) same as (This may or may not be true the first time will never be true thereafter) but after the statement, you have lost the handle, can't write and can't close
        if (1 == FileOpen("AnalArray.dat", FILE_BIN|FILE_READ))
    Open the file, test the result, SAVE the handle for use
        AnalHandle == FileOpen("AnalArray.dat", FILE_BIN|FILE_READ);
        if (AnalHandle < 0) // file does not exist.

  2. If you try to open the file for reading you can't write to it.
 
Pretty dumb mistake!!! eh