title: C File processing and matching help
body: <p>I have an assignment in college in which we are required to read in two .dat (aascii) files that we created with an earlier program, both of which are sorted.</p>
  
  <p>One is a client account file  which contains the clients balance and account number and name, the other is a transaction file that contains the account number and the transactions for that account.</p>
  
  <p>This program has to match the account numbers and create a new and updated clients file by adding the transaction amount to the balance of the client based on the accounts number.</p>
  
  <p>I have this working fine except for when there is a duplicate transaction for example if the transaction file contains 2 separate transactions for the 2nd client my code will print the updated balance after both transactions instead of a cumulative balance.</p>
  
  <p>I am just wondering if anyone can shed any light on how to solve this problem. My code is attached, thanks in advance.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;conio.h&gt;
  int main()
  { 
      int account,matches=0;     /* account number */
      char date[ 30 ]; /* account Date */
      double balance, saleamount,total=0, x;  /* account SaleAmount */
      int transaccount;
      char name [ 30 ];
  
  
  
      FILE *cfPtr;     /* cfPtr = clients.dat file pointer */
      FILE *ctPtr;    /* cfPtr = transaction.dat file pointer */
      FILE *cfPtr2;    /* cfPtr2 = new client file */
  
      cfPtr2 = fopen( "clientupdate.dat", "w" );
  
      /* fopen opens file; exits program if file cannot be opened */ 
      if ( ( cfPtr = fopen( "clients.dat", "r" ) ) == NULL ) {
          printf( "clients could not be opened\n" );
          fflush(stdin);
      } /* end if */
      else
          if( ( ctPtr = fopen( "transactions.dat", "r" ) ) == NULL)
          {
              printf( "File could not be opened\n" );
              fflush(stdin);
          }
      else { /* read account, date,name, balance and SaleAmount from files */
          fscanf( cfPtr, "%d%s%lf", &amp;account, &amp;name, &amp;balance );
          fflush(stdin);
  
          fscanf( ctPtr, "%d%s%lf", &amp;transaccount, &amp;date, &amp;saleamount );
          fflush(stdin);
  
          printf( "%-13s%-10s%s\n", "  Account", "Name", "Balance" );
          printf("|----------------------------------|\n");
          while( !feof(ctPtr))
          {   
  
              while( !feof(cfPtr) &amp;&amp;matches==0 )
              {   
  
                  if(account == transaccount)
                  {
                     matches=1;
                      total=0;
                      x = balance+saleamount;
                      total = total + x;
                      balance = total; 
  
                  printf("  %-10d%-10s%.2lf\n", account, name, total);
                  }
                  else
                  {
  
                      fscanf( cfPtr, "%d%s%lf", &amp;account, &amp;name, &amp;balance );
                      fflush(stdin);
                  }   
  
              }
              fprintf( cfPtr2, "%d %s %.2lf\n", account, name, total );
              fscanf( ctPtr, "%d%s%lf", &amp;transaccount, &amp;date, &amp;saleamount );
              fflush(stdin);
             matches=0;
  
          }
  
          fclose( cfPtr2 );
          getch();
  }
  </code></pre>