Cover Image for Banking Account System in C using File handling
214 views

Banking Account System in C using File handling

Creating a basic banking account system in C using file handling involves managing customer accounts, including account creation, deposits, withdrawals, and account information retrieval. Here’s a simple example of how you can implement this system:

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

// Structure to represent a bank account
struct BankAccount {
    int accountNumber;
    char name[100];
    double balance;
};

// Function to create a new bank account
void createAccount() {
    struct BankAccount newAccount;
    printf("Enter Account Number: ");
    scanf("%d", &newAccount.accountNumber);
    printf("Enter Name: ");
    scanf("%s", newAccount.name);
    printf("Enter Initial Balance: ");
    scanf("%lf", &newAccount.balance);

    FILE *file = fopen("accounts.txt", "a");
    if (file == NULL) {
        perror("Error opening file");
        exit(1);
    }

    fprintf(file, "%d %s %.2lf\n", newAccount.accountNumber, newAccount.name, newAccount.balance);
    fclose(file);

    printf("Account created successfully.\n");
}

// Function to deposit money into an account
void deposit() {
    int accountNumber;
    double amount;

    printf("Enter Account Number: ");
    scanf("%d", &accountNumber);
    printf("Enter Deposit Amount: ");
    scanf("%lf", &amount);

    FILE *file = fopen("accounts.txt", "r+");
    if (file == NULL) {
        perror("Error opening file");
        exit(1);
    }

    struct BankAccount account;
    int found = 0;

    while (fscanf(file, "%d %s %lf", &account.accountNumber, account.name, &account.balance) != EOF) {
        if (account.accountNumber == accountNumber) {
            account.balance += amount;
            fseek(file, -sizeof(account), SEEK_CUR);
            fprintf(file, "%d %s %.2lf", account.accountNumber, account.name, account.balance);
            found = 1;
            break;
        }
    }

    fclose(file);

    if (found) {
        printf("Deposit successful.\n");
    } else {
        printf("Account not found.\n");
    }
}

// Function to withdraw money from an account
void withdraw() {
    int accountNumber;
    double amount;

    printf("Enter Account Number: ");
    scanf("%d", &accountNumber);
    printf("Enter Withdraw Amount: ");
    scanf("%lf", &amount);

    FILE *file = fopen("accounts.txt", "r+");
    if (file == NULL) {
        perror("Error opening file");
        exit(1);
    }

    struct BankAccount account;
    int found = 0;

    while (fscanf(file, "%d %s %lf", &account.accountNumber, account.name, &account.balance) != EOF) {
        if (account.accountNumber == accountNumber) {
            if (account.balance >= amount) {
                account.balance -= amount;
                fseek(file, -sizeof(account), SEEK_CUR);
                fprintf(file, "%d %s %.2lf", account.accountNumber, account.name, account.balance);
                found = 1;
                break;
            } else {
                printf("Insufficient balance.\n");
                found = 1;
                break;
            }
        }
    }

    fclose(file);

    if (found) {
        printf("Withdrawal successful.\n");
    } else {
        printf("Account not found.\n");
    }
}

// Function to display account information
void displayAccount() {
    int accountNumber;

    printf("Enter Account Number: ");
    scanf("%d", &accountNumber);

    FILE *file = fopen("accounts.txt", "r");
    if (file == NULL) {
        perror("Error opening file");
        exit(1);
    }

    struct BankAccount account;
    int found = 0;

    while (fscanf(file, "%d %s %lf", &account.accountNumber, account.name, &account.balance) != EOF) {
        if (account.accountNumber == accountNumber) {
            printf("Account Number: %d\n", account.accountNumber);
            printf("Name: %s\n", account.name);
            printf("Balance: %.2lf\n", account.balance);
            found = 1;
            break;
        }
    }

    fclose(file);

    if (!found) {
        printf("Account not found.\n");
    }
}

int main() {
    int choice;

    while (1) {
        printf("\nBanking System Menu:\n");
        printf("1. Create Account\n");
        printf("2. Deposit\n");
        printf("3. Withdraw\n");
        printf("4. Display Account Info\n");
        printf("5. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                createAccount();
                break;
            case 2:
                deposit();
                break;
            case 3:
                withdraw();
                break;
            case 4:
                displayAccount();
                break;
            case 5:
                exit(0);
            default:
                printf("Invalid choice. Please try again.\n");
        }
    }

    return 0;
}

This code defines a basic banking system where you can create accounts, deposit money, withdraw money, and display account information. The account data is stored in a text file named “accounts.txt.”

Please note that this is a simplified example for educational purposes, and it lacks error handling for various scenarios. In a production system, you would need to add more robust error handling and validation for user input. Additionally, for a more secure system, consider using encryption for sensitive data and a database for storing account information.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS