Banking System in C (Complete Example with File Handling)


This project demonstrates a Banking System in C, allowing you to create accounts, deposit, withdraw, view, and delete accounts. It uses structures and file handling to manage bank customer records efficiently.

1. Features

  1. Create new bank accounts
  2. Deposit money
  3. Withdraw money
  4. View account details
  5. Delete an account
  6. Save and load records using files

2. Structure Definition


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct Account {
int accNo;
char name[50];
float balance;
};

3. File-Based Operations


#define FILENAME "accounts.dat"

// Create account
void createAccount() {
struct Account a;
FILE *fp = fopen(FILENAME, "ab");
if(!fp) { printf("Error opening file.\n"); return; }

printf("Enter Account Number: "); scanf("%d", &a.accNo);
printf("Enter Name: "); scanf(" %[^\n]", a.name);
printf("Enter Initial Balance: "); scanf("%f", &a.balance);

fwrite(&a, sizeof(a), 1, fp);
fclose(fp);
printf("Account created successfully!\n");
}

// View all accounts
void viewAccounts() {
struct Account a;
FILE *fp = fopen(FILENAME, "rb");
if(!fp) { printf("No records found.\n"); return; }

printf("\nAccNo\tName\tBalance\n");
while(fread(&a, sizeof(a), 1, fp)) {
printf("%d\t%s\t%.2f\n", a.accNo, a.name, a.balance);
}
fclose(fp);
}

// Deposit money
void deposit() {
int accNo;
float amount;
struct Account a;
FILE *fp = fopen(FILENAME, "rb+");
if(!fp) { printf("No records found.\n"); return; }

printf("Enter Account Number: "); scanf("%d", &accNo);
int found = 0;
while(fread(&a, sizeof(a), 1, fp)) {
if(a.accNo == accNo) {
printf("Enter Amount to Deposit: "); scanf("%f", &amount);
a.balance += amount;
fseek(fp, -sizeof(a), SEEK_CUR);
fwrite(&a, sizeof(a), 1, fp);
printf("Amount deposited successfully! New Balance: %.2f\n", a.balance);
found = 1;
break;
}
}
if(!found) printf("Account not found.\n");
fclose(fp);
}

// Withdraw money
void withdraw() {
int accNo;
float amount;
struct Account a;
FILE *fp = fopen(FILENAME, "rb+");
if(!fp) { printf("No records found.\n"); return; }

printf("Enter Account Number: "); scanf("%d", &accNo);
int found = 0;
while(fread(&a, sizeof(a), 1, fp)) {
if(a.accNo == accNo) {
printf("Enter Amount to Withdraw: "); scanf("%f", &amount);
if(amount > a.balance) {
printf("Insufficient Balance!\n");
} else {
a.balance -= amount;
fseek(fp, -sizeof(a), SEEK_CUR);
fwrite(&a, sizeof(a), 1, fp);
printf("Withdrawal successful! New Balance: %.2f\n", a.balance);
}
found = 1;
break;
}
}
if(!found) printf("Account not found.\n");
fclose(fp);
}

// Delete account
void deleteAccount() {
int accNo;
struct Account a;
FILE *fp = fopen(FILENAME, "rb");
FILE *temp = fopen("temp.dat", "wb");
if(!fp || !temp) { printf("File error.\n"); return; }

printf("Enter Account Number to Delete: "); scanf("%d", &accNo);
int found = 0;
while(fread(&a, sizeof(a), 1, fp)) {
if(a.accNo == accNo) {
found = 1;
continue; // skip copying to delete
}
fwrite(&a, sizeof(a), 1, temp);
}

fclose(fp);
fclose(temp);
remove(FILENAME);
rename("temp.dat", FILENAME);

if(found) printf("Account deleted successfully!\n");
else printf("Account not found.\n");
}

4. Main Menu


int main() {
int choice;
while(1) {
printf("\n--- Banking System ---\n");
printf("1. Create Account\n2. View Accounts\n3. Deposit\n4. Withdraw\n5. Delete Account\n6. Exit\n");
printf("Enter your choice: "); scanf("%d", &choice);

switch(choice) {
case 1: createAccount(); break;
case 2: viewAccounts(); break;
case 3: deposit(); break;
case 4: withdraw(); break;
case 5: deleteAccount(); break;
case 6: exit(0);
default: printf("Invalid choice!\n");
}
}
return 0;
}

5. Key Points to Remember

  1. Uses struct to store account information
  2. File handling ensures permanent storage (fwrite, fread, rb+)
  3. Menu-driven program allows easy banking operations
  4. Can be extended to include account search, interest calculation, or transaction history