Bank Management System in C with Source Code

Bank Management System in C

This mini project in C allows basic banking operations using file handling: deposit, withdraw, view account, and exit. It uses C structures and menu-based user interaction.

Features

  • Create new account
  • Deposit money
  • Withdraw money
  • Display account details
  • File handling using fopen(), fread(), fwrite()

Source Code (bank-management.c)


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

struct account {
    int acc_no;
    char name[100];
    float balance;
};

// Check if account exists
int accountExists(int accNo) {
    FILE *fp = fopen("bank.dat", "rb");
    struct account acc;
    while (fread(&acc, sizeof(acc), 1, fp)) {
        if (acc.acc_no == accNo) {
            fclose(fp);
            return 1; // Exists
        }
    }
    fclose(fp);
    return 0;
}

// Create new account
void createAccount() {
    FILE *fp = fopen("bank.dat", "ab");
    struct account acc;

    printf("Enter account number: ");
    scanf("%d", &acc.acc_no);

    if (accountExists(acc.acc_no)) {
        printf("Account number already exists!\n");
        fclose(fp);
        return;
    }

    printf("Enter name: ");
    scanf(" %[^\n]", acc.name);
    printf("Enter initial balance: ");
    scanf("%f", &acc.balance);

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

// Deposit money
void depositMoney() {
    FILE *fp = fopen("bank.dat", "rb+");
    struct account acc;
    int accNo;
    float amount;
    printf("Enter account number: ");
    scanf("%d", &accNo);

    while (fread(&acc, sizeof(acc), 1, fp)) {
        if (acc.acc_no == accNo) {
            printf("Enter amount to deposit: ");
            scanf("%f", &amount);
            acc.balance += amount;
            fseek(fp, -sizeof(acc), SEEK_CUR);
            fwrite(&acc, sizeof(acc), 1, fp);
            printf("Deposit successful!\n");
            fclose(fp);
            return;
        }
    }
    printf("Account not found.\n");
    fclose(fp);
}

// Withdraw money
void withdrawMoney() {
    FILE *fp = fopen("bank.dat", "rb+");
    struct account acc;
    int accNo;
    float amount;
    printf("Enter account number: ");
    scanf("%d", &accNo);

    while (fread(&acc, sizeof(acc), 1, fp)) {
        if (acc.acc_no == accNo) {
            printf("Enter amount to withdraw: ");
            scanf("%f", &amount);
            if (amount <= acc.balance) {
                acc.balance -= amount;
                fseek(fp, -sizeof(acc), SEEK_CUR);
                fwrite(&acc, sizeof(acc), 1, fp);
                printf("Withdrawal successful!\n");
            } else {
                printf("Insufficient balance.\n");
            }
            fclose(fp);
            return;
        }
    }
    printf("Account not found.\n");
    fclose(fp);
}

// Search account
void searchAccount() {
    FILE *fp = fopen("bank.dat", "rb");
    struct account acc;
    int accNo;
    printf("Enter account number to search: ");
    scanf("%d", &accNo);

    while (fread(&acc, sizeof(acc), 1, fp)) {
        if (acc.acc_no == accNo) {
            printf("\nAccount Found:\n");
            printf("Account No: %d\nName: %s\nBalance: %.2f\n", acc.acc_no, acc.name, acc.balance);
            fclose(fp);
            return;
        }
    }
    printf("Account not found.\n");
    fclose(fp);
}

// Delete account
void deleteAccount() {
    FILE *fp = fopen("bank.dat", "rb");
    FILE *temp = fopen("temp.dat", "wb");
    struct account acc;
    int accNo, found = 0;

    printf("Enter account number to delete: ");
    scanf("%d", &accNo);

    while (fread(&acc, sizeof(acc), 1, fp)) {
        if (acc.acc_no != accNo) {
            fwrite(&acc, sizeof(acc), 1, temp);
        } else {
            found = 1;
        }
    }

    fclose(fp);
    fclose(temp);

    remove("bank.dat");
    rename("temp.dat", "bank.dat");

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

// Display all accounts
void displayAccounts() {
    FILE *fp = fopen("bank.dat", "rb");
    struct account acc;
    printf("\n--  Account Details --\n");
    while (fread(&acc, sizeof(acc), 1, fp)) {
        printf("Account No: %d\nName: %s\nBalance: %.2f\n\n", acc.acc_no, acc.name, acc.balance);
    }
    fclose(fp);
}

// Main Menu
int main() {
    int choice;
    do {
        printf("\nBank Menu:\n");
        printf("1. Create Account\n");
        printf("2. Deposit\n");
        printf("3. Withdraw\n");
        printf("4. Display All Accounts\n");
        printf("5. Search Account\n");
        printf("6. Delete Account\n");
        printf("7. Exit\n");
        printf("Enter choice: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1: createAccount(); break;
            case 2: depositMoney(); break;
            case 3: withdrawMoney(); break;
            case 4: displayAccounts(); break;
            case 5: searchAccount(); break;
            case 6: deleteAccount(); break;
            case 7: printf("Thank you for using our banking system!\n"); break;
            default: printf("Invalid choice.\n");
        }
    } while (choice != 7);
    return 0;
}


        

Code Explanation

1. Header Files: The program includes <stdio.h>, <stdlib.h>, and <string.h> for standard I/O, memory operations, and string handling.

2. Structure Definition: A structure account is defined with 3 fields:

    int acc_no – Account number
    char name[100] – Account holder's name
    float balance – Account balance

3. accountExists(): Checks if the given account number already exists using fread() loop.

4. createAccount(): Asks user for account info and writes it to bank.dat file using fwrite().

5. depositMoney(): Finds account using account number, adds amount, and updates using fseek() + fwrite().

6. withdrawMoney(): Similar to deposit, but checks if balance is enough before withdrawing.

7. searchAccount(): Searches and displays specific account info.

8. deleteAccount(): Copies all records except the one to be deleted into a temp file, then replaces the old file.

9. displayAccounts(): Displays all accounts from bank.dat.

10. main(): Provides menu-driven interaction using switch-case.

1. Header Files: यह प्रोग्राम <stdio.h>, <stdlib.h> और <string.h> को शामिल करता है जो इनपुट-आउटपुट, मेमोरी मैनेजमेंट और स्ट्रिंग के लिए जरूरी हैं।

2. Structure Definition: account नाम का एक स्ट्रक्चर बनाया गया है जिसमें तीन फील्ड्स हैं:

  • int acc_no – खाता नंबर
  • char name[100] – खाता धारक का नाम
  • float balance – खाते का बैलेंस

3. accountExists(): यह फंक्शन जांचता है कि खाता पहले से मौजूद है या नहीं।

4. createAccount(): यूजर से डिटेल लेता है और bank.dat में fwrite() से सेव करता है।

5. depositMoney(): दिए गए अकाउंट नंबर पर अमाउंट जोड़ता है और फाइल में अपडेट करता है।

6. withdrawMoney(): बैलेंस चेक करके पैसे निकालता है, अगर पर्याप्त बैलेंस हो।

7. searchAccount(): एक अकाउंट को खोजता है और डिटेल दिखाता है।

8. deleteAccount(): दिए गए अकाउंट को छोड़कर बाकी सभी रिकॉर्ड को temp फाइल में कॉपी करता है और फिर फाइल को रिप्लेस कर देता है।

9. displayAccounts(): सभी खातों की डिटेल्स को दिखाता है।

10. main(): यह मेनू ड्रिवन प्रोग्राम है जो यूजर से ऑप्शन लेता है और उसी अनुसार फंक्शन कॉल करता है।