Bank account system in C using File handling

This article focuses on how to create a bank account system using C language and File handling in C.

Approach:
Let’s discuss the approach in detail, covering all the functions and their explanation in detail-

  1. Create a Menu in the main function and create different functions for the Menu, which will be called using switch case statements. There are four different functions-
    1. account()- This function is used to create a new account.
    2. transfermoney()- This function is used to transfer money to the account
    3. checkbalance()- This function is used to check the balance in the account.
    4. login()- This function is used to login into the account.

    Concepts of file handling will be used to store the data of the users, and then read all data from that particular file, store structures in a file because they are easy to write and read.

    Implementation:
    Let’s look at the C implementation of each of the modules of the program and finally consolidate all the modules together and create a full working program.

    1. Create a Bank Account-
    Take all the input from the user and make a structure for it to store the data in a file.

    // Function to create accounts // of users void account(void) < char password[20]; int passwordlength, i, seek = 0; char ch; FILE *fp, *fu; struct pass u1; struct userpass p1; struct userpass u2; // Opening file to // write data of a user fp = fopen("username.txt", "ab"); // Inputs system("cls"); printf("\n\n. CREATE ACCOUNT. "); printf("\n\nFIRST NAME.."); scanf("%s", &u1.fname); printf("\n\n\nLAST NAME.."); scanf("%s", &u1.lname); printf("\n\nFATHER's NAME.."); scanf("%s", &u1.fathname); printf("\n\nMOTHER's NAME.."); scanf("%s", &u1.mothname); printf("\n\nADDRESS.."); scanf("%s", &u1.address); printf("\n\nACCOUNT TYPE"); scanf("%s", &u1.typeaccount); printf("\n\nDATE OF BIRTH.."); printf("\nDATE-"); scanf("%d", &u1.date); printf("\nMONTH-"); scanf("%d", &u1.month); printf("\nYEAR-"); scanf("%d", &u1.year); printf("\n\nADHAR NUMBER"); scanf("%s", u1.adharnum); printf("\n\nPHONE NUMBER"); scanf("%s", u1.pnumber); printf("\n\nUSERNAME.. "); scanf("%s", &u1.username); printf("\n\nPASSWORD.."); // Taking password in the // form of stars for (i = 0; i < 50; i++) < ch = getch(); if (ch != 13) < password[i] = ch; ch = '*'; printf("%c", ch); >else break; > // Writing to the file fwrite(&u1, sizeof(u1), 1, fp); // Closing file fclose(fp); // Calling another function // after successful creation // of account accountcreated(); >

    2. Transfer Money-
    Take the username of another user to whom we want to transfer the money and open his record in the file and write the amount to the file.

    // Function to transfer // money from one user to // another void transfermoney(void) < int i, j; FILE *fm, *fp; struct pass u1; struct money m1; char usernamet[20]; char usernamep[20]; system("cls"); // Opening file in read mode to // read user's username fp = fopen("username.txt", "rb"); // Creating a another file // to write amount along with // username to which amount // is going to be transferred fm = fopen("mon.txt", "ab"); gotoxy(33, 4); printf("---- TRANSFER MONEY ----"); gotoxy(33, 5); printf("========================"); gotoxy(33, 11); printf("FROM (your username).. "); scanf("%s", &usernamet); gotoxy(33, 13); printf(" TO (username of person).."); scanf("%s", &usernamep); // Checking for username if it // is present in file or not while (fread(&u1, sizeof(u1), 1, fp)) < if (strcmp(usernamep, u1.username) == 0) < strcpy(m1.usernameto, u1.username); strcpy(m1.userpersonfrom, usernamet); >> gotoxy(33, 16); // Taking amount input printf("ENTER THE AMOUNT TO BE TRANSFERRED.."); scanf("%d", &m1.money1); // Writing to the file fwrite(&m1, sizeof(m1), 1, fm); gotoxy(0, 26); printf( "--------------------------------------------------" "--------------------------------------------"); gotoxy(0, 28); printf( "--------------------------------------------------" "--------------------------------------------"); gotoxy(0, 29); printf("transferring amount, Please wait.."); gotoxy(10, 27); for (i = 0; i < 70; i++) < for (j = 0; j < 1200000; j++) < j++; j--; >printf("*"); > gotoxy(33, 40); printf("AMOUNT SUCCESSFULLY TRANSFERRED. "); getch(); // Close the files fclose(fp); fclose(fm); // Function to return // to the home screen display(usernamet); >

    3. Check Balance-
    Opening a file in which all the transfer records are written and read them one by one and match the username passed in the function to fetch the correct transfer records.

    // Function to check balance // in users account void checkbalance(char username2[]) < system("cls"); FILE* fm; struct money m1; char ch; int i = 1, summoney = 0; // Opening amount file record fm = fopen("mon.txt", "rb"); int k = 5, l = 10; int m = 30, n = 10; int u = 60, v = 10; gotoxy(30, 2); printf("==== BALANCE DASHBOARD ===="); gotoxy(30, 3); printf("***************************"); gotoxy(k, l); printf("S no."); gotoxy(m, n); printf("TRANSACTION ID"); gotoxy(u, v); printf("AMOUNT"); // Reading username to // fetch the correct record while (fread(&m1, sizeof(m1), 1, fm)) < if (strcmp(username2, m1.usernameto) == 0) < gotoxy(k, ++l); printf("%d", i); i++; gotoxy(m, ++n); printf("%s", m1.userpersonfrom); gotoxy(u, ++v); printf("%d", m1.money1); // adding and // finding total money summoney = summoney + m1.money1; >> gotoxy(80, 10); printf("TOTAL AMOUNT"); gotoxy(80, 12); printf("%d", summoney); getch(); // Closing file after // reading it fclose(fm); display(username2); >

    4. Login Functionality-
    To add Login functionality, we are opening the file and matching the username provided by the user at the time of registration, and logging in to him if the username is correct and matches with the record present in our file.

    // Login function to check // the username of the user void login(void) < system("cls"); char username[50]; char password[50]; int i, j, k; char ch; FILE *fp, *fu; struct pass u1; struct userpass u2; // Opening file of // user data fp = fopen("username.txt", "rb"); if (fp == NULL) < printf("ERROR IN OPENING FILE"); >gotoxy(34, 2); printf(" ACCOUNT LOGIN "); gotoxy(7, 5); printf("***********************************************" "********************************"); gotoxy(35, 10); printf("==== LOG IN ===="); // Take input gotoxy(35, 12); printf("USERNAME.. "); scanf("%s", &username); gotoxy(35, 14); printf("PASSWORD.."); // Input the password for (i = 0; i < 50; i++) < ch = getch(); if (ch != 13) < password[i] = ch; ch = '*'; printf("%c", ch); >else break; > // Checking if username // exists in the file or not while (fread(&u1, sizeof(u1), 1, fp)) < if (strcmp(username, u1.username) == 0) < loginsu(); display(username); >> // Closing the file fclose(fp); > // Screen is shown after // successful login void loginsu(void) < int i; FILE* fp; struct pass u1; system("cls"); printf("Fetching account details. \n"); for (i = 0; i < 20000; i++) < i++; i--; >gotoxy(30, 10); printf("LOGIN SUCCESSFUL. "); gotoxy(0, 20); printf("Press enter to continue"); getch(); >

    Complete Program-
    Below is the complete C program for the bank system using the concepts of file handling-