C File processing and matching help -


i have assignment in college in required read in 2 .dat (aascii) files created earlier program, both of sorted.

one client account file contains clients balance , account number , name, other transaction file contains account number , transactions account.

this program has match account numbers , create new , updated clients file adding transaction amount balance of client based on accounts number.

i have working fine except when there duplicate transaction example if transaction file contains 2 separate transactions 2nd client code print updated balance after both transactions instead of cumulative balance.

i wondering if can shed light on how solve problem. code attached, 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 opened */      if ( ( cfptr = fopen( "clients.dat", "r" ) ) == null ) {         printf( "clients not opened\n" );         fflush(stdin);     } /* end if */     else         if( ( ctptr = fopen( "transactions.dat", "r" ) ) == null)         {             printf( "file not opened\n" );             fflush(stdin);         }     else { /* read account, date,name, balance , saleamount 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(); } 

although can't believe they're still teaching cobol (under guise of c), i'll out, since, unlike seek here, you've put in quite bit of work :-)

the trick not print after every update rather wait until account number transitions, print out previous final details.

and, since you'll reach end of transaction file while processing last account, you'll have 1 more time after exit loop.

here's detailed steps.


introduce 3 more variables, char lastname[30], int lastaccount , double lastbalance. ensure lastaccount initialised -1.

then code can catch transitions of account number checking values of lastaccount , account. storage of 3 fields ensure can print out details previous account though you've read in new account details.


before matches = 1, insert following code:

if (lastaccount != account) {     if (lastaccount != -1)         printf("  %-10d%-10s%.2lf\n", lastaccount, lastname, lastbalance);     lastaccount = account;     strcpy (lastname, name); } 

you'll need include string.h prototype strcpy.

this catch transitions of account number , print details last 1 (unless last 1 account number -1 of course since means there is no previous one). whether prints or not, sets next transition.


immediately after balance = total, add lastbalance = balance;. maintain recent balance current account can printed on transition.

unlike account number , name, has done every transaction rather every account. that's because account/name don't change account closing balance (for every single transaction).


outside of loop, before fclose (cfptr2);, put code:

if (lastaccount != -1)     printf("  %-10d%-10s%.2lf\n", lastaccount, lastname, lastbalance); 

this prints details final account, assuming there processed @ all. mentioned earlier, needed because you'll reach end of file without having done transition due changed account number.


and, please, final request:

  • get rid of conio.h , use getchar() instead of getch().
  • if possible, ditch turbo c , use gcc - it's so better :-)
  • don't fflush(stdin).
  • use canonical int main (void) ....
  • return main (although think that's relaxed in later versions of standard).

i won't post actual code (unless guarantee it's not homework) following transcript shows changes should enough fix current issues:

$ cat clients.dat 1 pax 4.2 2 roger 0  $ cat transactions.dat 1 1/2/3 112.3 1 2/3/4 -22.1 2 3/4/5 7  $ ./qq   account    name      balance |----------------------------------|   1         pax       94.40   2         roger     7.00 

for it's worth, i'm not sure quite agree treating tranasction file primary element. since job copy account file needed modifications (as specified in transaction file), i'd prefer process account file primary unit.

it seems cleaner me way.

and, in interests of completeness, here's code came with. wouldn't suggest handing in assignment since there numerous things mark reduced, things matter whole lot less in real world educators have particular bee in bonnet :-) things multiple return points, using #define instead of enum, , on.

#include <stdio.h> #include <string.h>  #define hdr_line 0 #define hdr_full 1  static void outheader (int withheading) {     printf ("+-%-6s-+-%-10s-+-%-6s-+-%-6s-+-%-6s-+\n",         "------", "----------", "------", "------", "------");     if (withheading)         printf ("| %-6s | %-10s | %-6s | %-6s | %-6s |\n",             " acct ", "   name   ", " date ", "amount", "balnce"); }  static void closefiles (file *pfcli, file *pftxn, file *pfnew) {     if (pfcli) fclose (pfcli);     if (pftxn) fclose (pftxn);     if (pfnew) fclose (pfnew); }  static int openfiles (file **pfcli, file **pftxn, file **pfnew) {     *pfcli = *pftxn = *pfnew = null;      if ((*pfcli = fopen ("clients.dat", "r")) == null) {         printf ("file clients.dat not opened\n");         closefiles (*pfcli, *pftxn, *pfnew);         return 0;     }      if ((*pftxn = fopen ("transactions.dat", "r")) == null) {         printf ("file transactions.dat not opened\n");         closefiles (*pfcli, *pftxn, *pfnew);         return 0;     }      if ((*pfnew = fopen ("clientupdate.dat", "w")) == null) {         printf ("file clientupdate.dat not opened\n");         closefiles (*pfcli, *pftxn, *pfnew);         return 0;     }      return 1; }  static void outcli (int acct, char *nm, double bal, char *ind) {     if (acct == -1)         printf ("| %6s | %-10s | %6s | %6s | %6.2lf | %s\n",             "", nm, "", "", bal, ind);     else         printf ("| %6d | %-10s | %6s | %6s | %6.2lf | %s\n",             acct, nm, "", "", bal, ind); }  static void outtxn (char *date, double amt, double bal) {     printf ("| %6s | %10s | %-6s | %6.2lf | %6.2lf |\n",         "", "", date, amt, bal); }  static void gettxn (file *fh, int *acct, char *date, double *amt) {     if (feof (fh))         *acct = -1;     else         fscanf (fh, "%d%s%lf\n", acct, date, amt); }  static void getcli (file *fh, int *last, int *acct, char *nm, double *bal) {     if (*last != -1)         outcli (-1, "", *bal, "-->");     *last = *acct;     fscanf (fh, "%d%s%lf\n", acct, nm, bal); }  int main (void) {      int cli_acct, txn_acct, last_acct = -1;     char txn_date[10], cli_nm[10];     double cli_bal, txn_amt;     file *fcli, *ftxn, *fnew;      // open files, output header , load first transaction.      if (!openfiles (&fcli, &ftxn, &fnew))         return 1;      outheader (hdr_full);      gettxn (ftxn, &txn_acct, txn_date, &txn_amt);      // process every account.      while (!feof (fcli)) {         // bring in account , print starting balance.          getcli (fcli, &last_acct, &cli_acct, cli_nm, &cli_bal);         outheader (hdr_line);         outcli (cli_acct, cli_nm, cli_bal, "<--");          // while account not yet txn, output , read new.          while (cli_acct < txn_acct) {             fprintf (fnew, "%d %s %.2lf\n", cli_acct, cli_nm, cli_bal);             outcli (-1, "", cli_bal, "-->");             outheader (hdr_line);             fscanf (fcli, "%d%s%lf\n", &cli_acct, &cli_nm, &cli_bal);             outcli (cli_acct, cli_nm, cli_bal, "<--");         }          // if don't match, have orphan transaction.          if ((cli_acct != txn_acct) && (txn_acct != -1)) {             printf ("orphan transaction account # %d\n", txn_acct);             closefiles (fcli, ftxn, fnew);             return 1;         }          // while account , transaction match, process transaction lines.          while (cli_acct == txn_acct) {             cli_bal = cli_bal + txn_amt;             outtxn (txn_date, txn_amt, cli_bal);             if (feof (ftxn))                 txn_acct = -1;             else                 fscanf (ftxn, "%d%s%lf\n", &txn_acct, &txn_date, &txn_amt);         }         fprintf (fnew, "%d %s %.2lf\n", cli_acct, cli_nm, cli_bal);     }      // output final lines if needed.      if (last_acct != -1)         outcli (-1, "", cli_bal, "-->");     outheader (hdr_line);      // close files , exit successfully.      closefiles (fcli, ftxn, fnew);     return 0; } 

given clients.dat of:

0 mary 99.9 1 pax 4.2 2 bob 7 4 roger 0 5 nobody 9 

and transactions.dat of:

1 1/2/3 112.3 1 2/3/4 -22.1 4 3/4/5 7 

this outputs:

+--------+------------+--------+--------+--------+ |  acct  |    name    |  date  | amount | balnce | +--------+------------+--------+--------+--------+ |      0 | mary       |        |        |  99.90 | <-- |        |            |        |        |  99.90 | --> +--------+------------+--------+--------+--------+ |      1 | pax        |        |        |   4.20 | <-- |        |            | 1/2/3  | 112.30 | 116.50 | |        |            | 2/3/4  | -22.10 |  94.40 | |        |            |        |        |  94.40 | --> +--------+------------+--------+--------+--------+ |      2 | bob        |        |        |   7.00 | <-- |        |            |        |        |   7.00 | --> +--------+------------+--------+--------+--------+ |      4 | roger      |        |        |   0.00 | <-- |        |            | 3/4/5  |   7.00 |   7.00 | |        |            |        |        |   7.00 | --> +--------+------------+--------+--------+--------+ |      5 | nobody     |        |        |   9.00 | <-- |        |            |        |        |   9.00 | --> +--------+------------+--------+--------+--------+ 

and creates clientupdate.dat of:

0 mary 99.90 1 pax 94.40 2 bob 7.00 4 roger 7.00 5 nobody 9.00 

feel free use code purpose other passing off own homework.


Comments

Popular posts from this blog

python - Scipy curvefit RuntimeError:Optimal parameters not found: Number of calls to function has reached maxfev = 1000 -

c# - How to add a new treeview at the selected node? -

java - netbeans "Please wait - classpath scanning in progress..." -