FIXED!! File I/O: why this (little) fragment is not working?

 

Hi,


I'm trying to save active orders (Tickets) OnDeinit() into a binary file but is not working. (Note: The handle is created but the file is left blank.)

Can anybody tell me why? Here is the piece of code:

//prepare to write data...
   
   int handle = FileOpen("orders.bin",FILE_BIN|FILE_WRITE|FILE_READ);
   
   if (handle<0) 
      Print("err: ",GetLastError());
   else
      FileSeek(handle,0,SEEK_SET);//set pointer at beginning of file
   
   //write active orders count
   FileWriteInteger(handle,activeOrders,LONG_VALUE);
   //advance pointer (4 bytes) to next position
   FileSeek(handle,4,SEEK_CUR);

   //write orders one by one
   for (int i = 1; i < activeOrders; i++) {
      OrderSelect( i, SELECT_BY_POS, MODE_TRADES );
      FileWriteInteger(handle,OrderTicket(),LONG_VALUE);
      FileSeek(handle,4,SEEK_CUR);//advance pointer (4 bytes) to next position
      }
   
   FileClose(handle);
 
robotalfa:

Hi,


I'm trying to save active orders (Tickets) OnDeinit() into a binary file but is not working. (Note: The handle is created but the file is left blank.)

Can anybody tell me why? Here is the piece of code:


for (int i = 1; i < activeOrders; i++) will not catch the first order ... for (int i=0;i<activeOrders;i++)

But better will be otherwise i=activeorders-1;i>=0;i--

FileSeek after FileWrite you dont need...

 
EADeveloper:


for (int i = 1; i < activeOrders; i++) will not catch the first order ... for (int i=0;i<activeOrders;i++)

But better will be otherwise i=activeorders-1;i>=0;i--

FileSeek after FileWrite you dont need...



let me try and see if it works...

P.S. Still, this fix is not writing to file either.

 
robotalfa:

I'm trying to save active orders (Tickets) OnDeinit() into a binary file but is not working. [...]

This isn't answering your question (which EADeveloper has already done), but it will make debugging, testing, inspection, and manual overrides about a billion times easier if you save the list as a text file, e.g. with each ticket number on a separate line. You then need slightly more complex code to parse the text file when reading it back in, but I'd be very surprised if this didn't save you time in the long run.
 
jjc:
[..] if you save the list as a text file, e.g. with each ticket number on a separate line. [..]
Would you show me a brief example, please!
 
robotalfa:
Would you show me a brief example, please!

You can just get MT4 to read and write the list as a CSV file which only happens to have one entry per line. For example, you can write the file as follows:

   int f = FileOpen("test.txt", FILE_CSV | FILE_WRITE);
   
   FileWrite(f, 123);
   FileWrite(f, 456);
   FileWrite(f, 789);

   FileClose(f);

You then read it back in as though it was a CSV file:

   int f = FileOpen("test.txt", FILE_CSV | FILE_READ);
   while (!FileIsEnding(f)) {
      int Ticket = FileReadNumber(f);
      Print("Read: " + Ticket);
   }
   FileClose(f);   

The only complication is that MT4 reads an extra entry (with Ticket == 0). You can either watch for this and ignore it, or you can use code such as the following which explicitly checks for blank lines:

   int f = FileOpen("test.txt", FILE_CSV | FILE_READ);
   while (!FileIsEnding(f)) {
      string strLine = FileReadString(f);
      if (strLine != "") {
         int Ticket = StrToInteger(strLine);
         Print("Read: " + Ticket);
      }
   }
   FileClose(f);
 
jjc:
[...]



Thanks so much.. appreciated! Will try!
 

Problem is FIXED. The cause was the size of the file name apparently: robotorders.bin(BAD: too long?!), orders.bin(GOOD!)


THX 2 ALL!!

Reason: