Truncating or Limiting Bin Files - MQL4

 

Dear all,

My limiting knowledge of programming is giving me a hard time with the piece of code below:


void OnTick()



  {

   double PriceDiff = MarketInfo("AUDCAD",(MODE_ASK));

        

   int fileHandle2 = FileOpen("Myfile.bin",FILE_BIN|FILE_READ | FILE_WRITE);

       

   FileSeek(fileHandle2, 0, SEEK_END); // Write at the end of the file.

   double data2 = PriceDiff;

   FileWriteDouble(fileHandle2,data2);

   FileClose(fileHandle2);    

   double arr[];

   string path= "";

   ResetLastError();

   int file_handle=FileOpen("Myfile.bin",FILE_READ|FILE_BIN);

   if(file_handle!=INVALID_HANDLE)

     {

      FileReadArray(file_handle,arr);

      //--- receive the array size

      int size=ArraySize(arr);

      //--- print data from the array

      for(int i=0;i<size;i++);

      Comment("Total data = ",size);

      //--- close the file

      FileClose(file_handle);



The above, most of which taken from online sources and adapted to my needs, is simply storing tick price data into a bin file (on every tick) and closing file.
The next paragraph opens that file and puts that data into an array to be used at a later stage.
The problem is that this way the file just keeps on getting larger and larger. Am not sure how to solve it and limit the file to a max of 1000 ticks, for example.


Documentation on MQL5: Constants, Enumerations and Structures / Input/Output Constants / File Opening Flags
Documentation on MQL5: Constants, Enumerations and Structures / Input/Output Constants / File Opening Flags
  • www.mql5.com
CSV file (all its elements are converted to strings of the appropriate type, Unicode or ANSI, and separated by separator). Flag is used in FileOpen(). Shared access for reading from several programs. Flag is used in FileOpen(), but it does not replace the necessity to indicate FILE_WRITE and/or the FILE_READ flag when opening a file. Shared...
 
muserfish:The problem is that this way the file just keeps on getting larger and larger. Am not sure how to solve it and limit the file to a max of 1000 ticks, for example.
  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum 2019.05.06
              Messages Editor

  2. That's not the problem, that's the symptom. You must decide what you need to do at max size. Stop, make it a queue, make it a circular buffer, delete and restart, etc.
  3. Until you decide in concrete terms, no one can help you. The terms come from how you use the data.
 
William Roeder:
  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum 2019.05.06
              Messages Editor

  2. That's not the problem, that's the symptom. You must decide what you need to do at max size. Stop, make it a queue, make it a circular buffer, delete and restart, etc.
  3. Until you decide in concrete terms, no one can help you. The terms come from how you use the data.
Thanks William. I did edit the original post.
As per regards to points 2 and 3, my fault I did not specify. A circular buffer is what im looking for.

Example:  Maxsize = 5;

A, B, C, D, E is the data in the file and subsequently in the array. Once maxsize is reached, which in this case it has, it keeps storing next data in the file and eliminating the bottom (oldest) data.
so next ticks:
B, C, D, E, F
C, D, E, F, G  etc etc.
Allowing the lines in file to be alway at Maxsize.  
Reason: