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.
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.
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.
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.
I am just wondering if anyone can shed any light on how to solve this problem. My code is attached, thanks in advance.
#include <stdio.h>
#include <conio.h>
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", &account, &name, &balance );
fflush(stdin);
fscanf( ctPtr, "%d%s%lf", &transaccount, &date, &saleamount );
fflush(stdin);
printf( "%-13s%-10s%s\n", " Account", "Name", "Balance" );
printf("|----------------------------------|\n");
while( !feof(ctPtr))
{
while( !feof(cfPtr) &&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", &account, &name, &balance );
fflush(stdin);
}
}
fprintf( cfPtr2, "%d %s %.2lf\n", account, name, total );
fscanf( ctPtr, "%d%s%lf", &transaccount, &date, &saleamount );
fflush(stdin);
matches=0;
}
fclose( cfPtr2 );
getch();
}
With help from pax and a good while playing with my own code i came up with this working solution. Thanks again for the help , Much appreciated.