Note that there are some explanatory texts on larger screens.

plurals
  1. POC File updating and creating help
    primarykey
    data
    text
    <p>I have two .dat(ascii) files. Both sorted.</p> <p>1: a clients file containing ; account number , name ,balance 2: a transaction file containing; account number ,date,saleamount(transaction amount)</p> <p>What i am trying to accomplish is create a new updated clients file which has updated balances for the clients based on adding or subtracting the saleamount of the matching transaction.</p> <p>My code so far enables me to :</p> <p>1: if there are not more than one transactions for a client the code runs perfectly writes the .dat file with the clients and their updated balances.</p> <p>2:if there are more than one transactions for a client my code will run almost perfectly as it will print to the screen the updated clients and accounts for eg:</p> <p>1 james 540.00 2 john 762.00 3 paul 414.00 4 sam 502.00</p> <p>will be displayed , but as there are two transactions for john the created .dat file while contain</p> <p>1 james 540.00 2 john 662.00 2 john 762.00 3 paul 414.00 4 sam 502.00</p> <p>My problem lies here, I need to find a way of having the created .dat contain only one line for each client ( account number)</p> <p>My code is attached any help would be greatly appreciated.</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;string.h&gt; int main(void) { int account, matches=0; /* account number */ char date[ 30 ]; /* account Date */ double balance, saleamount,total=0, temp;; /* account SaleAmount */ int transaccount; char name [ 30 ]; char lastname[30]; int lastaccount=-1; double lastbalance; 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) { if (lastaccount != account) { if (lastaccount != -1) printf(" %-10d%-10s%.2lf\n", lastaccount, lastname, lastbalance); lastaccount = account; strcpy (lastname, name); } matches=1; total=0; temp = balance+saleamount; total = total + temp; balance = total; lastbalance = balance; } 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; } } if (lastaccount != -1) printf(" %-10d%-10s%.2lf\n", lastaccount, lastname, lastbalance); fclose( cfPtr2 ); getchar(); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload