C File updating and creating help -


i have 2 .dat(ascii) files. both sorted.

1: clients file containing ; account number , name ,balance 2: transaction file containing; account number ,date,saleamount(transaction amount)

what trying accomplish create new updated clients file has updated balances clients based on adding or subtracting saleamount of matching transaction.

my code far enables me :

1: if there not more 1 transactions client code runs writes .dat file clients , updated balances.

2:if there more 1 transactions client code run print screen updated clients , accounts eg:

1 james 540.00 2 john 762.00 3 paul 414.00 4 sam 502.00

will displayed , there 2 transactions john created .dat file while contain

1 james 540.00 2 john 662.00 2 john 762.00 3 paul 414.00 4 sam 502.00

my problem lies here, need find way of having created .dat contain 1 line each client ( account number)

my code attached appreciated.

#include <stdio.h> #include <string.h> 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 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)                 {                     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", &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;          }     }        if (lastaccount != -1)     printf("  %-10d%-10s%.2lf\n", lastaccount, lastname, lastbalance);         fclose( cfptr2 );         getchar();     return 0; } 

as promised, here quick exmaple of father-son update. have not bothered checking file opening errors have them in code, , real application need check more eof fscanf in order detect invalid file formatting.

#include <stdlib.h> #include <stdio.h>  #define bool int #define false 0 #define true !false  #define max_account_number 99999  typedef struct {  int accountnumber;  char accountname[30];  float accounttotal; } clientrecordtype;  typedef struct {  int accountnumber;  char transactiondate[30];  float transactionamount; } transactionrecordtype;  file *oldclientfile; file *newclientfile; file *transactionfile;   void getnextclient(file *p_clientfile, clientrecordtype *p_clientrecord) {  if (eof == fscanf(p_clientfile, "%d%s%f",                     &p_clientrecord->accountnumber,                    &p_clientrecord->accountname,                    &p_clientrecord->accounttotal))    p_clientrecord->accountnumber = max_account_number; }   void getnexttransaction(file *p_transactionfile,                         transactionrecordtype *p_transactionrecord) {  if (eof == fscanf(p_transactionfile, "%d%s%f",                     &p_transactionrecord->accountnumber,                    &p_transactionrecord->transactiondate,                    &p_transactionrecord->transactionamount))    p_transactionrecord->accountnumber = max_account_number; }   void writeupdatedclientrecord(file *p_newclientfile,                               clientrecordtype *p_clientrecord) {  fprintf(p_newclientfile, "%d %s %.2f ",          p_clientrecord->accountnumber,          p_clientrecord->accountname,          p_clientrecord->accounttotal); }    bool performtransactionupdate(file *p_oldclientfile,                               file *p_newclientfile,                               file *p_transactionfile) {  clientrecordtype clientrecord;  transactionrecordtype transactionrecord;   getnextclient(p_oldclientfile, &clientrecord);  getnexttransaction(p_transactionfile, &transactionrecord);  while (max_account_number != clientrecord.accountnumber)  {   if (clientrecord.accountnumber == transactionrecord.accountnumber)   {    clientrecord.accounttotal += transactionrecord.transactionamount;    getnexttransaction(p_transactionfile, &transactionrecord);   }   else if (clientrecord.accountnumber < transactionrecord.accountnumber)   {    writeupdatedclientrecord(p_newclientfile, &clientrecord);    getnextclient(p_oldclientfile, &clientrecord);   }   else   {    return false;   }  }  if (max_account_number != transactionrecord.accountnumber) return false;  return true; }   int main(int argc, char *argv[]) {  oldclientfile = fopen("clients.dat", "r");  newclientfile = fopen("newclients.dat", "w");  transactionfile = fopen("transactions.dat", "r");   if (performtransactionupdate(oldclientfile,                               newclientfile,                               transactionfile))    printf("\nupdate completed without errors!\n");  else    printf("\nunsorted files or invalid transactions encountered\n");   close(newclientfile);  close(oldclientfile);  close(transactionfile);  return 0; } 

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..." -