Writing reading and saving in a file with 2 dimensional arrays - what am I doing wrong please

 

Please find below my code and log for loading an array, writing it to a file and reading it back (no problem) UNTIL I try resize the array to add another row to store more data.

I get no errors but data entered from an order into the array is not there when I try to read it back again. HELP!!! been trying to fix it for over a week now so bound to be something I do not

understand or/and I am being really stupid.

#include <TC.mq4>

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

int deinit()
 {
  FileDelete(AnalHandle);
 }
 
int start()
 {
  OldRow = 0;
  Row = 1;
  AnalHandle = FileOpen("Analysis.dat", FILE_BIN|FILE_READ);
  if (AnalHandle < 1)
   {
    Print("No file exists so build MyArray in Memory");
    for (i = OldRow; i < Row; i++)
     {
      for (j = 0; j < Column; j++)
       {
        MyArray[i,j] = (j + 1)*2;
        Print("Memory = MyArray[i,j] ", MyArray[i,j]);
       }
     }
    Print("Save Array that is in Memory to Analysis.dat File");
    AnalHandle = FileOpen("Analysis.dat", FILE_BIN|FILE_WRITE);
    if (AnalHandle < 1)
     {
      Print("Cannot Build Analysis File");
      return(0);
     }
    FileWriteArray(AnalHandle, MyArray, OldRow, Column);
    FileFlush(AnalHandle);
    FileClose(AnalHandle);
    Print("Readback array from file");
    AnalHandle = FileOpen("Analysis.dat", FILE_BIN|FILE_READ);
    if (AnalHandle < 1)
     {
      Print("cant read back file");
     }
    FileFlush(AnalHandle);
    FileClose(AnalHandle);
    for (i = OldRow; i < Row; i++)
     {
      for (j = 0; j < Column; j++)
       {
        Print("Memory = MyArray[i,j] ", MyArray[i,j]);
       }
     }
   }
  if (OrdersTotal() == 0)
   {
    OpenGBP();
   }
  return(0);  
 }

void OpenGBP()
 {
  if (TradeBusy() < 0) 
   {
    Print("Trade is still busy ");
    return(0);
   }
  MyTicket = OrderSend("GBPUSDm", OP_BUY, 1, Ask, 5, 0, 0, NULL, 0, 0, Blue);
  if (MyTicket < 0)
   {
    Print("Error ", GetLastError(), " On attempt to open a buy trade");
    TradeNotBusy();
    return(0);
   }
  TradeNotBusy();
Print("Before adding a row --- MyTicket = ", MyTicket, " OldRow = ", OldRow, " Row = ", Row, " ArraySize = ", ArraySize(MyArray));
Print("Adding a Row");
  OldRow = Row;
  Row = ArrayResize(MyArray, Row + 1)/Column;
Print("After adding a row --- MyTicket = ", MyTicket, " OldRow = ", OldRow, " Row = ", Row, " ArraySize = ", ArraySize(MyArray));
  MyArray[Row,1] = MyTicket;
Print("MyArray[Row,1] = ", MyArray[Row,1]);
  MyArray[Row,2] = Ask; 
Print("MyArray[Row,1] = ", MyArray[Row,1]);
  MyArray[Row,3] = Bid;
Print("MyArray[Row,1] = ", MyArray[Row,1]);
Print("Printing whole array");
  for (i = 0; i < Row; i++)
   {
    for (j = 0; j < Column; j++)
     {
      Print(MyArray[i,j]);
     }
   }
 }



LOG

14:04:57 Compiling 'Help1'
14:06:59 Help1: filename for FileDelete function must be a string
14:06:59 Help1 GBPUSDm,H1: deinitialized
14:06:59 Help1 GBPUSDm,H1: uninit reason 2
14:06:59 Help1 GBPUSDm,H1: loaded successfully
14:08:05 Help1: filename for FileDelete function must be a string
14:08:05 Help1 GBPUSDm,H1: deinitialized
14:08:05 Help1 GBPUSDm,H1: uninit reason 2
14:08:05 Help1 GBPUSDm,H1: loaded successfully
14:08:26 Help1: filename for FileDelete function must be a string
14:08:26 Help1 GBPUSDm,H1: deinitialized
14:08:26 Help1 GBPUSDm,H1: uninit reason 2
14:08:26 Help1 GBPUSDm,H1: loaded successfully
14:09:21 Help1: filename for FileDelete function must be a string
14:09:21 Help1 GBPUSDm,H1: deinitialized
14:09:21 Help1 GBPUSDm,H1: uninit reason 1
14:09:21 Help1 GBPUSDm,H1: removed
14:10:17 Help1 GBPUSDm,H1: loaded successfully
14:10:28 Help1 GBPUSDm,H1: No file exists so build MyArray in Memory
14:10:28 Help1 GBPUSDm,H1: Memory = MyArray[i,j] 2
14:10:28 Help1 GBPUSDm,H1: Memory = MyArray[i,j] 4
14:10:28 Help1 GBPUSDm,H1: Memory = MyArray[i,j] 6
14:10:28 Help1 GBPUSDm,H1: Save Array that is in Memory to Analysis.dat File
14:10:28 Help1 GBPUSDm,H1: Readback array from file
14:10:28 Help1 GBPUSDm,H1: Memory = MyArray[i,j] 2
14:10:28 Help1 GBPUSDm,H1: Memory = MyArray[i,j] 4
14:10:28 Help1 GBPUSDm,H1: Memory = MyArray[i,j] 6
14:10:30 Help1 GBPUSDm,H1: open #51768171 buy 1.00 GBPUSDm at 1.64401 ok
14:10:30 Help1 GBPUSDm,H1: Before adding a row --- MyTicket = 51768171 OldRow = 0 Row = 1 ArraySize = 3
14:10:30 Help1 GBPUSDm,H1: Adding a Row
14:10:30 Help1 GBPUSDm,H1: After adding a row --- MyTicket = 51768171 OldRow = 1 Row = 2 ArraySize = 6
14:10:30 Help1 GBPUSDm,H1: MyArray[Row,1] = 0
14:10:30 Help1 GBPUSDm,H1: MyArray[Row,1] = 0
14:10:30 Help1 GBPUSDm,H1: MyArray[Row,1] = 0
14:10:30 Help1 GBPUSDm,H1: Printing whole array
14:10:30 Help1 GBPUSDm,H1: 2
14:10:30 Help1 GBPUSDm,H1: 4
14:10:30 Help1 GBPUSDm,H1: 6
14:10:30 Help1 GBPUSDm,H1: 0
14:10:30 Help1 GBPUSDm,H1: 0
14:10:30 Help1 GBPUSDm,H1: 0
 
BigAl:

Please find below my code and log for loading an array, writing it to a file and reading it back (no problem) UNTIL I try resize the array to add another row to store more data. [...]


If you declare or resize an array so that it has 2 elements (e.g. ArrayResize(arr, 2)) then those elements are 0 and 1, not 1 and 2.

As far as I can see you are resizing the array from 1 and 2, and then trying to write data into arr[2] when only arr[0] and arr[1] are valid.

 
BigAl:

Please find below my code and log for loading an array, writing it to a file and reading it back (no problem) UNTIL I try resize the array to add another row to store more data.

I get no errors but data entered from an order into the array is not there when I try to read it back again. HELP!!! been trying to fix it for over a week now so bound to be something I do not

understand or/and I am being really stupid.


Read the documentation . . . FileDelete() string filename


If you have 1 row the first row number is 0 not 1 . . .
 
gchrmt4:


If you declare or resize an array so that it has 2 elements (e.g. ArrayResize(arr, 2)) then those elements are 0 and 1, not 1 and 2.

As far as I can see you are resizing the array from 1 and 2, and then trying to write data into arr[2] when only arr[0] and arr[1] are valid.


Thanks please see my final post on this subject.
 
RaptorUK:
Read the documentation . . . FileDelete() string filename


If you have 1 row the first row number is 0 not 1 . . .

Thanks please see my final post on this subject
 

Thanks to the contributors gchrmt4: and RaptorUK:

There are two basic errors in each of 3 code lines

MyArray[Row,1] = MyTicket;
MyArray[Row,2] = Ask;
MyArray[Row,3] = Bid;

Should be

MyArray[OldRow,0] = MyTicket;
MyArray[OldRow,1] = Ask;
MyArray[OldRow,2] = Bid;

Thanks again Guys

Reason: