In this minor assignment, you will write a complete C program that will accept two command-line arguments for the names of an account file and a transaction file. Your program will as mutually exclusive operations (1) read the data from the account file into a dynamically allocated list, (2) read the data from the transaction file and update the inmemory list of accounts, and (3) write the updated data to the account file in the same format as the original account file. This means, for example, that you do not write the updated data to the account file until after all the transactions have been read and updated in memory.

The account file will contain a variable number of user accounts with three fields on each line, one account per line: (1) account number as an integral value fixed at 8 characters in length, (2) account name supporting at most 20-characters containing no spaces, and (3) account balance with a leading '$' character formatted in dollars and cents. Note that if the account balance is negative, the leading '$' will be preceded by a negative sign.

The transaction file will contain a variable number of transactions with two fields, one transaction per line: (1) either account number or account name (not both) with the account number and account name as described in the account file, and (2) a signed, real number representing a monetary amount, formatted to two decimal places, but no $' character.

Some assumptions and general rules

  • You may assume a maximum string length of 20 (i.e., 21 with the null terminator) for the file names and account names.
  • If your program is given an invalid number of arguments, you will print out a usage statement to standard error and terminate your program with an exit status of EXIT_FAILURE.
  • You will define a struct that contains all the data for a single account. During the execution of your program, you are to maintain a dynamically allocated inmemory list (such as an array or linked list) of this structure type. However, you may only allocate the amount of memory needed and each account file used to test your program may contain a variable number of accounts.
  • Except for constants and the type definition for your struct, no global variables should be used.
  • You must free all memory that you dynamically allocate when no longer needed.
  • Add error checking where needed (e.g., after opening file, allocating memory, etc.) and take the appropriate action, such as sending an error message to standard error and terminating your program. For a transaction with an unknown account number or account name, you will print an informative error message to standard error and continue processing the transaction file.
  • Though you do not have to worry if the account balance is negative, you should handle negative values in the account file correctly.
  • If you have any questions about the functionality, please contact your instructor.

Sample Output

$ more acctFile
01014791 Johnson-Smith $1389.01
01020811 Thompson $653.36
01028352 Smith $7180.18
01048741 Jones $6926.65
01058388 Williams $1284.38
01078192 McReynolds $38.23
01183485 Webber $1983.87
02817471 Martinez $4028.22
03184971 Thomas $3164.26
$ more xactFile
01020811 -25.95
Smith 231.12
01028352 -19.99
Jones 132.82
01014791 -83.45
McReynolds -5.24
Martinez 481.83
01045737 37.90
01058388 106.57
Webber 14.63
Jonas -138.52
Johnson-Smith -89.42
01078192 -40.00
$ ./a.out
usage: ./a.out < account file> < transact file>
$ ./a.out acct
usage: ./a.out < account file> < transact file>
$ ./a.out acct xactFile
error: unable to open file acct
$ ./a.out acctFile xact File
error: unknown account number: 01045737
error: unknown account number: Jonas
$ more acctFile 01014791
Johnson-Smith $1216.14 01020811
Thompson $627.41 01028352
Smith $7391.31 01048741
Jones $7059.47 01058388
Williams $1390.95 01078192
McReynolds -$7.01 01183485
Webber $1998.50 02817471
Martinez $4510.05 03184971
Thomas $3164.26
$ more acctFile2
01078192 McReynolds -$52.25
$ more xactFile2 McReynolds -16.24
$ ./a.out acctFile2 xactFile2
$ more acctFile2
01078192 McReynolds
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.