Why is this still writing to file?

 

I created a function to rewrite my order history for the month of March to an existing file, but I only want it to rewrite if the OrderTicket() does not exist in the file.  The first time I run it, it writes all March's orders to separate files based on strategy, and it works just fine. But when I run it again, it writes every order again.  The line where I wrote Print(OrderTicket()," true"); prints out true for every ticket number, but then still writes???  EDIT:  I tried including Print(GetLastError()) after the while loop, error code returns 0 error.

void Rewrite()
{

   ResetLastError();

   for(int i=OrdersHistoryTotal()-1;i>=0;i--)
   {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
         continue;
      if(!OurMagicNumber(OrderMagicNumber()))
         continue;
      if(TimeMonth(OrderOpenTime())<3)
         continue;
      int strat=(int)StringSubstr(OrderComment(),1,1); //defines strategy #
      bool exist=false;
      FileSeek(filehandle_[strat],0,SEEK_SET);
      while(!FileIsEnding(filehandle_[strat]) && !IsStopped())
      {
         if(FileReadString(filehandle_[strat])==(string)OrderTicket())
         {
            exist=true;
            Print(OrderTicket()," true");
            break;
         }
      }
      if(!exist)
         WriteTradeHistory(OrderTicket(),"rewrite");
   }
}
 
Ian Tavener:

I created a function to rewrite my order history for the month of March to an existing file, but I only want it to rewrite if the OrderTicket() does not exist in the file.  The first time I run it, it writes all March's orders to separate files based on strategy, and it works just fine. But when I run it again, it writes every order again.  The line where I wrote Print(OrderTicket()," true"); prints out true for every ticket number, but then still writes???  EDIT:  I tried including Print(GetLastError()) after the while loop, error code returns 0 error.

I think I found my fix:    Not entirely sure why it works, but it does. 

void Rewrite()
{

   ResetLastError();

   for(int i=OrdersHistoryTotal()-1;i>=0;i--)
   {
      if(!OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
         continue;
      if(!OurMagicNumber(OrderMagicNumber()))
         continue;
      if(TimeMonth(OrderOpenTime())<3)
         continue;
      int strat=(int)StringSubstr(OrderComment(),1,1);
      bool exist=false;
      FileSeek(filehandle_[strat],0,SEEK_SET);
      while(!FileIsEnding(filehandle_[strat]) && !IsStopped())
      {
         string finder=FileReadString(filehandle_[strat]);
         if(finder!=(string)OrderTicket())
            continue;
         else
         {
            exist=true;
            break;
         }
      }
      if(!exist)
         WriteTradeHistory(OrderTicket(),"rewrite");
   }
   MakeMyTerminal("History Rewrite Complete");
}