How do you implement a binary file, accounts.dat which should hold a maximum num
ID: 3708979 • Letter: H
Question
How do you implement a binary file, accounts.dat which should hold a maximum number of 50 accounts. Those accounts can be stored as an array, but any change in any account should be immediately reflected in the data file not only on exit.
In the accounts.dat the following is stored for each customer: first name, middle initial, last name, account number(6 digits), account balance
The accounts.dat file is being changed by a c program which can do the following: deposit, withdrawal, add account, remove account, balance inquiry, and view accounts.
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
#define MAX 100 // for number of customers
#define MAXN 20 // for name
// Structure definition for CustomerAccount
struct CustomerAccount
{
// Data member to store data
char firstName[MAXN];
char middleName[MAXN];
char lastName[MAXN];
int accountNo;
float balance;
};// End of structure
// Renames the structure
typedef struct CustomerAccount cusAcc;
// Function to display menu and return user choice
int menu()
{
// To store user choice
int choice;
// Displays menu
printf(" 1 - Deposit 2 - Withdrawal 3 - Add Account 4 - Remove Account 5 - Balance Inquiry 6 - View Accounts 7 - Exit.");
// Accepts user choice
printf(" Enter your choice: ");
scanf("%d", &choice);
// Returns user choice
return choice;
}// End of function
// Function to read file and stores in customer array of object
int readFile(cusAcc customer[])
{
// File pointer declared
FILE *fileRead;
// Record counter
int counter = 0;
// Opens the file for reading and checks whether it can be opened or not
if ((fileRead = fopen("CustomerAccount.txt", "r")) == NULL)
{
// If unable to open shows error message
printf("Error! in opening file for reading");
// Program exits if the file pointer returns NULL.
exit(1);
}// End of if condition
// Loops till end of file
while(!feof(fileRead))
{
// Reads data from file and stores array of objects customer's object counter index position
fscanf(fileRead,"%s", customer[counter].firstName);
fscanf(fileRead,"%s", customer[counter].middleName);
fscanf(fileRead,"%s", customer[counter].lastName);
fscanf(fileRead,"%d", &customer[counter].accountNo);
fscanf(fileRead,"%f", &customer[counter].balance);
// Increase the record counter by one
counter++;
}// End of while condition
// Closes the file
fclose(fileRead);
// Returns number of records
return counter;
}// End of function
// Function to write the array of objects information to file
void writeFile(cusAcc customer[], int len)
{
// Loop variable
int c;
// File pointer declared
FILE *fileWrite;
// Opens the file for writing and checks whether it can be opened or not
if ((fileWrite = fopen("CustomerAccount.txt", "w")) == NULL)
{
// If unable to open shows error message
printf("Error! in opening file for reading");
// Program exits if the file pointer returns NULL.
exit(1);
}// End of if condition
// Loops till number of records
for(c = 0; c < len; c++)
{
// Writes customer object's c index position data to file
fprintf(fileWrite,"%s ", customer[c].firstName);
fprintf(fileWrite,"%s ", customer[c].middleName);
fprintf(fileWrite,"%s ", customer[c].lastName);
fprintf(fileWrite,"%d ", customer[c].accountNo);
// Checks if last record to write
if(c == len - 1)
// Don't write new line
fprintf(fileWrite,"%f", customer[c].balance);
// Otherwise write new line
else
fprintf(fileWrite,"%f ", customer[c].balance);
}// End of while condition
// Closes the file
fclose(fileWrite);
}// End of function
// Function to display all account information
void displayAccount(cusAcc customer[], int len)
{
// Loop variable
int c;
// Loops till number of records
for(c = 0; c < len; c++)
// Displays each account information
printf(" First Name: %s Middle Name: %s Last Name: %s Account Number: %d Balance: %.2f",
customer[c].firstName, customer[c].middleName, customer[c].lastName, customer[c].accountNo, customer[c].balance);
}// End of function
// Function to add account information
void addAccount(cusAcc cus[], int *len)
{
// Accepts the account information
printf(" Enter the first name: ");
scanf("%s", cus[*len].firstName);
printf(" Enter the middle name: ");
scanf("%s", cus[*len].middleName);
printf(" Enter the last name: ");
scanf("%s", cus[*len].lastName);
printf(" Enter the account number: ");
scanf("%d", &cus[*len].accountNo);
printf(" Enter the account balance: ");
scanf("%f", &cus[*len].balance);
// Increase the record counter by one
*len = *len + 1;
}// End of function
// Function to return the index position of the account number found otherwise returns -1 for not found
int searchAccout(cusAcc cus[], int len, int accNo)
{
// Loop variable
int c;
// To store found index position
int index = -1;
// Loops till number of records
for(c = 0; c < len; c++)
{
// Checks if the current index position account number is equals to the account passed as parameter
if(cus[c].accountNo == accNo)
{
// Assigns the loop variable value as the current index position
index = c;
// Comes out of the loop
break;
}// End of if condition
}// End of for loop
// Returns the index position
return index;
}// End of function
// Function to withdraw amount from a account
void withdrawAmount(cusAcc cus[], int len)
{
// To store the account number entered by the user
int accNo;
// To store the amount withdrawal
float amount;
// To store the index position of found account number
int found = 0;
// Accepts a account number from the user
printf(" Enter the account number: ");
scanf("%d", &accNo);
// Calls the function to search the account number and stores the return index position
found = searchAccout(cus, len, accNo);
// Checks if the found value is -1 display record not found
if(found == -1)
printf(" Account number %d not found.", accNo);
// Otherwise, account number found
else
{
// Accept the amount to withdraw
printf(" Enter the withdrawal amount: ");
scanf("%f", &amount);
// Checks the found customer balance is less than the withdraw amount entered by the user
if(cus[found].balance < amount)
// Display error message with current balance and withdraw amount entered by the user
printf(" Insufficient balance: Current balance: %.2f Withdrawal amount: %.2f", cus[found].balance, amount);
// Otherwise sufficient balance is available
else
{
// Subtract the withdrawal amount from the found account's balance
cus[found].balance -= amount;
printf(" Withdraw successful: ");
}// End of inner else
}// End of outer else
}// End of function
// Function to deposit amount in an account
void depositAmount(cusAcc cus[], int len)
{
// To store the account number entered by the user
int accNo;
// To store the amount deposit
float amount;
// To store the index position of found account number
int found = 0;
// Accepts a account number from the user
printf(" Enter the account number: ");
scanf("%d", &accNo);
// Calls the function to search the account number and stores the return index position
found = searchAccout(cus, len, accNo);
// Checks if the found value is -1 display record not found
if(found == -1)
printf(" Account number %d not found.", accNo);
// Otherwise, account number found
else
{
// Accept the amount to deposit
printf(" Enter the deposit amount: ");
scanf("%f", &amount);
// Adds the deposit amount to the found account's balance
cus[found].balance += amount;
printf(" Deposit successful: ");
}// End of else
}// End of function
// Function to remove an account
void removeAccount(cusAcc cus[], int *len)
{
// To store the account number entered by the user
int accNo;
// Loop variable
int c;
// To store the index position of found account number
int found = 0;
// Accepts a account number from the user
printf(" Enter the account number: ");
scanf("%d", &accNo);
// Calls the function to search the account number and stores the return index position
found = searchAccout(cus, *len, accNo);
// Checks if the found value is -1 display record not found
if(found == -1)
printf(" Account number %d not found.", accNo);
// Otherwise, account number found
else
{
// Loops from the found index position till end of the record
for(c = found; c < *len; c++)
// Stores the next record to its previous position
cus[c] = cus[c + 1];
// Decrease the record length by one
*len = *len - 1;
printf(" Account number %d deleted successfully.", accNo);
}// End of else
}// End of function
// Function to display current balance of an account
void balanceInquiry(cusAcc cus[], int len)
{
// To store the account number entered by the user
int accNo;
// To store the index position of found account number
int found = 0;
// Accepts a account number from the user
printf(" Enter the account number: ");
scanf("%d", &accNo);
// Calls the function to search the account number and stores the return index position
found = searchAccout(cus, len, accNo);
// Checks if the found value is -1 display record not found
if(found == -1)
printf(" Account number %d not found.", accNo);
// Otherwise, account number found and displays it current balance
else
printf(" Account number %d current balance %.2f.", cus[found].accountNo, cus[found].balance);
}// End of function
// main function definition
int main()
{
// To store number of records
int numberOfRecords = 0;
// Creates an array of objects for structure cusACC
cusAcc customer[MAX];
// Loops till user choice is not 7
do
{
// Calls the function to display menu, accept user choice and return user choice
// Based on the return choice value calls the appropriate function
switch(menu())
{
case 1:
// Calls the function to read file contents
numberOfRecords = readFile(customer);
// Calls the function to deposit amount
depositAmount(customer, numberOfRecords);
// Calls the function to write to file
writeFile(customer, numberOfRecords);
break;
case 2:
// Calls the function to read file contents
numberOfRecords = readFile(customer);
withdrawAmount(customer, numberOfRecords);
// Calls the function to write to file
writeFile(customer, numberOfRecords);
break;
case 3:
// Calls the function to read file contents
numberOfRecords = readFile(customer);
addAccount(customer, &numberOfRecords);
// Calls the function to write to file
writeFile(customer, numberOfRecords);
break;
case 4:
// Calls the function to read file contents
numberOfRecords = readFile(customer);
removeAccount(customer, &numberOfRecords);
// Calls the function to write to file
writeFile(customer, numberOfRecords);
break;
case 5:
// Calls the function to read file contents
numberOfRecords = readFile(customer);
balanceInquiry(customer, numberOfRecords);
break;
break;
case 6:
// Calls the function to read file contents
numberOfRecords = readFile(customer);
displayAccount(customer, numberOfRecords);
break;
case 7:
exit(0);
default:
printf(" Invalid choice!");
break;
}// End of switch case
}while(1); // End of do - while loop
}// End of main function
Sample Output:
1 - Deposit
2 - Withdrawal
3 - Add Account
4 - Remove Account
5 - Balance Inquiry
6 - View Accounts
7 - Exit.
Enter your choice: 6
First Name: Pyari Middle Name: Mohan Last Name: Sahu
Account Number: 123321
Balance: 10000.00
First Name: Bishnu Middle Name: Prasad Last Name: Sahu
Account Number: 122451
Balance: 20000.00
First Name: Sasmita Middle Name: Kumari Last Name: Panda
Account Number: 223322
Balance: 9000.00
1 - Deposit
2 - Withdrawal
3 - Add Account
4 - Remove Account
5 - Balance Inquiry
6 - View Accounts
7 - Exit.
Enter your choice: 1
Enter the account number: 111111
Account number 111111 not found.
1 - Deposit
2 - Withdrawal
3 - Add Account
4 - Remove Account
5 - Balance Inquiry
6 - View Accounts
7 - Exit.
Enter your choice: 1
Enter the account number: 123321
Enter the deposit amount: 500
Deposit successful:
1 - Deposit
2 - Withdrawal
3 - Add Account
4 - Remove Account
5 - Balance Inquiry
6 - View Accounts
7 - Exit.
Enter your choice: 5
Enter the account number: 123321
Account number 123321 current balance 10500.00.
1 - Deposit
2 - Withdrawal
3 - Add Account
4 - Remove Account
5 - Balance Inquiry
6 - View Accounts
7 - Exit.
Enter your choice: 2
Enter the account number: 232323
Account number 232323 not found.
1 - Deposit
2 - Withdrawal
3 - Add Account
4 - Remove Account
5 - Balance Inquiry
6 - View Accounts
7 - Exit.
Enter your choice: 2
Enter the account number: 223322
Enter the withdrawal amount: 1000
Withdraw successful:
1 - Deposit
2 - Withdrawal
3 - Add Account
4 - Remove Account
5 - Balance Inquiry
6 - View Accounts
7 - Exit.
Enter your choice: 5
Enter the account number: 223322
Account number 223322 current balance 8000.00.
1 - Deposit
2 - Withdrawal
3 - Add Account
4 - Remove Account
5 - Balance Inquiry
6 - View Accounts
7 - Exit.
Enter your choice: 3
Enter the first name: Ram
Enter the middle name: Kumar
Enter the last name: Panda
Enter the account number: 114455
Enter the account balance: 2000
1 - Deposit
2 - Withdrawal
3 - Add Account
4 - Remove Account
5 - Balance Inquiry
6 - View Accounts
7 - Exit.
Enter your choice: 6
First Name: Pyari Middle Name: Mohan Last Name: Sahu
Account Number: 123321
Balance: 10500.00
First Name: Bishnu Middle Name: Prasad Last Name: Sahu
Account Number: 122451
Balance: 20000.00
First Name: Sasmita Middle Name: Kumari Last Name: Panda
Account Number: 223322
Balance: 8000.00
First Name: Ram Middle Name: Kumar Last Name: Panda
Account Number: 114455
Balance: 2000.00
1 - Deposit
2 - Withdrawal
3 - Add Account
4 - Remove Account
5 - Balance Inquiry
6 - View Accounts
7 - Exit.
Enter your choice: 4
Enter the account number: 122451
Account number 122451 deleted successfully.
1 - Deposit
2 - Withdrawal
3 - Add Account
4 - Remove Account
5 - Balance Inquiry
6 - View Accounts
7 - Exit.
Enter your choice: 6
First Name: Pyari Middle Name: Mohan Last Name: Sahu
Account Number: 123321
Balance: 10500.00
First Name: Sasmita Middle Name: Kumari Last Name: Panda
Account Number: 223322
Balance: 8000.00
First Name: Ram Middle Name: Kumar Last Name: Panda
Account Number: 114455
Balance: 2000.00
1 - Deposit
2 - Withdrawal
3 - Add Account
4 - Remove Account
5 - Balance Inquiry
6 - View Accounts
7 - Exit.
Enter your choice: 7
CustomerAccount.txt contents
Pyari
Mohan
Sahu
123321
10500.000000
Sasmita
Kumari
Panda
223322
8000.000000
Ram
Kumar
Panda
114455
2000.000000
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.