Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3933256
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:34:49+00:00 2026-05-19T23:34:49+00:00

I have an assignment in college in which we are required to read in

  • 0

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();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T23:34:49+00:00Added an answer on May 19, 2026 at 11:34 pm

    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.

    #include <stdio.h>
    #include <string.h>
    int main(void)
    { 
        int account;     /* account number */
        char date[ 30 ]; /* account Date */
        double balance, saleamount; /* account SaleAmount */
        int transaccount;
        char name [ 30 ];
        int newaccount;
        char newname[ 30];
        double newbalance;
        int match=0;
    
    
    
        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" );
    
        } /* end if */
        else
            if( ( ctPtr = fopen( "transactions.dat", "r" ) ) == NULL)
            {
                printf( "File could not be opened\n" );
    
            }
        else { /* read account, date,name, balance and SaleAmount from files */
            fscanf( cfPtr, "%d%s%lf", &account, &name, &balance );
    
    
            fscanf( ctPtr, "%d%s%lf", &transaccount, &date, &saleamount );
            fscanf( cfPtr2, "%d%s%lf", &newaccount, &newname, &newbalance );
            while ( !feof( cfPtr ) )
            {
                transaccount=1;
                newbalance=balance;
                while ( !feof( ctPtr ) )
                {
                    if(transaccount==account&&match==0)
                    {
                     newbalance = balance + saleamount;
                     match++;
                    }
                    else
                        if(transaccount==account&&match>0)
                         newbalance = newbalance + saleamount;
    
                    fscanf( ctPtr, "%d%s%lf", &transaccount, date, &saleamount );
                }
                rewind( ctPtr);
                match=0;
    
            fprintf( cfPtr2, "%d %s %.2f\n", account, name, newbalance );
            fscanf( cfPtr, "%d%s%lf", &account, name, &balance );
            }
    
            rewind( cfPtr2);
        }
            fclose( cfPtr2 );
            fclose( cfPtr );
            fclose( ctPtr );
            getchar();
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For an assignment in college, we have to make a script in Perl that
I have this assignment to prove that this problem: Finite alphabet £, two strings
I have the following code that I am working on for my college assignment.
I am a student in college and have an assignment which requires finding large
I have an assignment to create a program that converts deci (-32,768 through 32,767)
I have an assignment in which I have to allow a user to plot
I have an assignment that wants me to write an ternary search algorithm and
My college course just started going over mongodb and we have an assignment to
I have an assignment to be done in college, a piece asks for the
I'm doing a college assignment in XNA to make a virtual piano. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.