Download MetaTrader 5
To add comments, please log in or register
Find and download Code Base apps via MetaTrader. It is fast!
BigAl
487
BigAl 2013.12.11 15:34 

I am probably doing something wrong. I am trying to build an array, store it in a file, and read it back. However, it seems to work but the data read back is incorrect!! so obviously it does not work please see the attached code and the associated expert data.

int AnalHandle = 0, Row = 1, Column = 17, i = 0, j = 0; 
double MyArray[1,17];
int start()
 {
  AnalHandle = FileOpen("AnalArray.dat", FILE_BIN|FILE_READ);
  // Test if the file exists, if it does not exist build it
  if (AnalHandle < 1)
   {
    Print("Building Starting AnalArray");
    for (i = 0; i < Row; i++)
     {
      for (j = 0; j < Column; j++)
       {
        MyArray[i,j] = j+1;
        // Show the contents of each column of the Array
        Print("MyArray[i,j] = ", MyArray[i,j]);
       }
     }
    // Open a file to store the Array
    AnalHandle = FileOpen("AnalArray.dat", FILE_BIN|FILE_WRITE);
    if (AnalHandle < 1)
     {
      Print("Cannot Build Starting AnalArray");
      return(0);
     }
    // Store the Array
    for (i = 0; i < Row; i++)
     {
      for (j = 0; j < Column; j++)
       {
        FileWriteArray(AnalHandle, MyArray, i, j);
        Print("Writing MyArray i = ", i, " j = ", j, " content = ",  MyArray[i,j]);
       }
     }
    FileFlush(AnalHandle);
    FileClose(AnalHandle);
    Print("AnalArray Start Position saved in file");
   }
  AnalHandle = FileOpen("AnalArray.dat", FILE_BIN|FILE_READ);
  if (AnalHandle < 1)
   {
    Print("No File To Read");
    return(0);
   } 
  //So open the file and read the array contents
  for (i = 0; i < Row; i++)
   {
    for (j = 0; j < Column; j++)
     {
      FileReadArray(AnalHandle, MyArray, i, j);
      Print("Reading MyArray i = ", i, " j = ", j, " content is = ",  MyArray[i,j]);
     }
   }
  FileFlush(AnalHandle);
  FileClose(AnalHandle);
 }

Expert Data:
17:13:31 Reader GBPUSDm,H1: loaded successfully
17:13:38 Reader GBPUSDm,H1: Building Starting AnalArray
17:13:38 Reader GBPUSDm,H1: MyArray[i,j] = 1
17:13:38 Reader GBPUSDm,H1: MyArray[i,j] = 2
17:13:38 Reader GBPUSDm,H1: MyArray[i,j] = 3
17:13:38 Reader GBPUSDm,H1: MyArray[i,j] = 4
17:13:38 Reader GBPUSDm,H1: MyArray[i,j] = 5
17:13:38 Reader GBPUSDm,H1: MyArray[i,j] = 6
17:13:38 Reader GBPUSDm,H1: MyArray[i,j] = 7
17:13:38 Reader GBPUSDm,H1: MyArray[i,j] = 8
17:13:38 Reader GBPUSDm,H1: MyArray[i,j] = 9
17:13:38 Reader GBPUSDm,H1: MyArray[i,j] = 10
17:13:38 Reader GBPUSDm,H1: MyArray[i,j] = 11
17:13:38 Reader GBPUSDm,H1: MyArray[i,j] = 12
17:13:38 Reader GBPUSDm,H1: MyArray[i,j] = 13
17:13:38 Reader GBPUSDm,H1: MyArray[i,j] = 14
17:13:38 Reader GBPUSDm,H1: MyArray[i,j] = 15
17:13:38 Reader GBPUSDm,H1: MyArray[i,j] = 16
17:13:38 Reader GBPUSDm,H1: MyArray[i,j] = 17
17:13:38 Reader GBPUSDm,H1: Writing MyArray i = 0 j = 0 content = 1
17:13:38 Reader GBPUSDm,H1: Writing MyArray i = 0 j = 1 content = 2
17:13:38 Reader GBPUSDm,H1: Writing MyArray i = 0 j = 2 content = 3
17:13:38 Reader GBPUSDm,H1: Writing MyArray i = 0 j = 3 content = 4
17:13:38 Reader GBPUSDm,H1: Writing MyArray i = 0 j = 4 content = 5
17:13:38 Reader GBPUSDm,H1: Writing MyArray i = 0 j = 5 content = 6
17:13:38 Reader GBPUSDm,H1: Writing MyArray i = 0 j = 6 content = 7
17:13:38 Reader GBPUSDm,H1: Writing MyArray i = 0 j = 7 content = 8
17:13:38 Reader GBPUSDm,H1: Writing MyArray i = 0 j = 8 content = 9
17:13:38 Reader GBPUSDm,H1: Writing MyArray i = 0 j = 9 content = 10
17:13:38 Reader GBPUSDm,H1: Writing MyArray i = 0 j = 10 content = 11
17:13:38 Reader GBPUSDm,H1: Writing MyArray i = 0 j = 11 content = 12
17:13:38 Reader GBPUSDm,H1: Writing MyArray i = 0 j = 12 content = 13
17:13:38 Reader GBPUSDm,H1: Writing MyArray i = 0 j = 13 content = 14
17:13:38 Reader GBPUSDm,H1: Writing MyArray i = 0 j = 14 content = 15
17:13:38 Reader GBPUSDm,H1: Writing MyArray i = 0 j = 15 content = 16
17:13:38 Reader GBPUSDm,H1: Writing MyArray i = 0 j = 16 content = 17
17:13:38 Reader GBPUSDm,H1: AnalArray Start Position saved in file
17:13:38 Reader GBPUSDm,H1: Reading MyArray i = 0 j = 0 content is = 1
17:13:38 Reader GBPUSDm,H1: Reading MyArray i = 0 j = 1 content is = 1
17:13:38 Reader GBPUSDm,H1: Reading MyArray i = 0 j = 2 content is = 2
17:13:38 Reader GBPUSDm,H1: Reading MyArray i = 0 j = 3 content is = 1
17:13:38 Reader GBPUSDm,H1: Reading MyArray i = 0 j = 4 content is = 2
17:13:38 Reader GBPUSDm,H1: Reading MyArray i = 0 j = 5 content is = 3
17:13:38 Reader GBPUSDm,H1: Reading MyArray i = 0 j = 6 content is = 1
17:13:38 Reader GBPUSDm,H1: Reading MyArray i = 0 j = 7 content is = 2
17:13:38 Reader GBPUSDm,H1: Reading MyArray i = 0 j = 8 content is = 3
17:13:38 Reader GBPUSDm,H1: Reading MyArray i = 0 j = 9 content is = 4
17:13:38 Reader GBPUSDm,H1: Reading MyArray i = 0 j = 10 content is = 1
17:13:38 Reader GBPUSDm,H1: Reading MyArray i = 0 j = 11 content is = 2
17:13:38 Reader GBPUSDm,H1: Reading MyArray i = 0 j = 12 content is = 3
17:13:38 Reader GBPUSDm,H1: Reading MyArray i = 0 j = 13 content is = 4
17:13:38 Reader GBPUSDm,H1: Reading MyArray i = 0 j = 14 content is = 5
17:13:38 Reader GBPUSDm,H1: Reading MyArray i = 0 j = 15 content is = 1
17:13:38 Reader GBPUSDm,H1: Reading MyArray i = 0 j = 16 content is = 2
GreenMoney
379
GreenMoney 2013.12.11 16:13  
BigAl:

I am probably doing something wrong. I am trying to build an array, store it in a file, and read it back. However, it seems to work but the data read back is incorrect!! so obviously it does not work please see the attached code and the associated expert data.

Yes...your use of FileWriteArray() and FileReadArray() is incorrect. FileWriteArray() and FileReadArray() should not be given dimension index (i.e., i and j); rather you must supply the starting point in the array and the count of elements you wish to write and read. So, if your variable is defined as NewArray[1, 17] and you want to write that array to a file, then:

FileWriteArray(AnalHandle, MyArray, 0, 17);

The above writes MyArray starting at index 0 for a count of 17 elements to the file defined by AnalHandle.

The same for FileReadArray():

FileReadArray(AnalHandle, NewMyArray, 0, 17);

The above reads 17 elements from file defined by AnalHandle and stores those elements in NewMyArray starting at index 0.

BigAl
487
BigAl 2013.12.11 16:19  
Thirteen:

Yes...your use of FileWriteArray() and FileReadArray() is incorrect. FileWriteArray() and FileReadArray() should not be given dimension index (i.e., i and j); rather you must supply the starting point in the array and the count of elements you wish to write and read. So, if your variable is defined as NewArray[1, 17] and you want to write that array to a file, then:

The above writes MyArray starting at index 0 for a count of 17 elements to the file defined by AnalHandle.

The same for FileReadArray():

The above reads 17 elements from file defined by AnalHandle and stores those elements in NewMyArray starting at index 0.


OK. Thanks for that I have been chasing my tail round in circles for a couple of days trying to figure it out.
Ian Venner
2583
Ian Venner 2013.12.12 05:04  
 Starting AnalArray

sounds like a painful medical procedure ...

/
To add comments, please log in or register