Coders Packet

File-Handling Basics In C++ using Account Management Example.

By Garvit Jethwani

In this Packet, We are using file handling concepts in C++ to keep the data persistence even after the program execution. We are taking an Account class for this.

We have Taken An Account Class having the variables Account_Number, Balance, and Name as a Record.

Now, whenever we get the input by the user by Enter_Account_Details() function, it will be stored in RAM during program execution and will Eventually be deleted as soon as the program completely executed. So, to keep the data entered by the user stored we are using file-handling concepts.

We are including fstream library for being able to create both objects of output and the input stream. Two types of objects of ofstream and ifstream are used. We are basically five functionalities we are able to do in the program related to operating with the file -

1) For Storing Information in the file we have Store_Account() function in which we are storing the data entered by the user in the Account.txt file. If this file does not exist already, then it will be automatically created in the current directory and then a record will be entered at the end of the file.

2) For displaying All accounts in the file we are using the Display_All_Accounts() function, which displays all the records in the Account.txt file.

3) For Searching Account by name we have Search_Account() function which takes pointer of name to be searched as a parameter. It will display the Account Record matching with the name. 

4) For Deleting a record matching by the name we have the Delete_Account() function which also takes the pointer of the name of the account holder to be deleted as a parameter. It will delete the record matching with the name from the file. For this purpose, a temp_file.txt is used which will be renamed as Account.txt after all the records not matching with the name.

5) For Updating a record matching by the name we have the Update_Account() function which also takes the pointer of the name of the account to be updated as a parameter. It will update the record with the new details entered by the user.

 

Note:  Account.txt should be placed in the same directory of the program. If not then use the path address of the file. 

Also, the Account.txt file may not understandable by us, only the program can correctly fetch the details from that.

 

 

 

 

 

Download project

Reviews Report

Submitted by Garvit Jethwani (Gjgarvit)

Download packets of source code on Coders Packet