C++ program to show binary file operations using classes and Object
C++ program to show binary file operation using classes and Objects and perform Addition, Deletion, Modification, searching, Display of records.
/* Program to write student record using class and binary files made by : rakesh kumar */ #include #include #include using namespace std; class student{ private: int roll; char name[30]; char address[100]; public: void input(){ fflush(stdin); cout<<"\n Enter Roll No:"; cin>>roll; fflush(stdin); cout<<"\n Enter Name :"; cin>>name; fflush(stdin); cout<<"\n Enter address :"; cin>>address; } void output(){ cout<<"\n\n Roll No :"<<roll; cout<<"\n Name :"<<name; cout<<"\n Address :"<<address; } int ret_roll() { return roll; } }; //global functions to write data in a binary file void write_data() { ofstream fout("student.dat",ios::app|ios::binary); student s; s.input(); fout.write((char*)&s,sizeof(student)); fout.close(); } //global functions to write data in a binary file void read_data() { ifstream fin("student.dat",ios::binary); student s; while(fin.read((char*)&s,sizeof(student))) s.output(); fin.close(); } //global function to modify record in a binary file void modify_data() { int troll; int found=0; student s; ofstream fout("temp.dat",ios::binary); ifstream fin("student.dat",ios::binary); cout<<"\n Enter roll No to modify :"; cin>>troll; while(fin.read((char*)&s,sizeof(student))) { if(troll==s.ret_roll()) { cout<<"\n Enter New Values \n"; s.input(); found++; } fout.write((char*)&s,sizeof(student)); } fin.close(); fout.close(); remove("student.dat"); rename("temp.dat","student.dat"); if(found!=0) cout<<"\n Record Modified...."; else cout<<"\n Record does not exist..."; } //global function to delete record from a binary file void delete_data() { int troll; int found=0; student s; ofstream fout("temp.dat",ios::binary); ifstream fin ("student.dat",ios::binary); cout<<"\n Enter roll No to delele :"; cin>>troll; while(fin.read((char*)&s,sizeof(student))) { if(troll!=s.ret_roll()) fout.write((char*)&s,sizeof(student)); } fin.close(); fout.close(); remove("student.dat"); rename("temp.dat","student.dat"); if(found!=0) cout<<"\n Record Deleted...."; else cout<<"\n Record does not exist..."; } int main(){ int choice; do{ system("cls"); cout<<"\n\n\n\n\t\t\t File operations"; cout<<"\n\n\t\t\t1. Add New Record"; cout<<"\n\n\t\t\t2. Modify Record"; cout<<"\n\n\t\t\t3. Delete Record"; cout<<"\n\n\t\t\t4. Display Records"; cout<<"\n\n\t\t\t5. Quit Application"; cout<<"\n\n\n\n\t\t Enter your choice (1..5) :"; cin>>choice; switch(choice) { case 1: write_data(); break; case 2: modify_data(); break; case 3: delete_data(); break; case 4: read_data(); getch(); break; case 5: break; default: cout<<"\n Wrong Choice....Try again"; getch(); } }while(choice!=5); return 0; }
The output of the program is as follows. The whole program was developed on Windows 10 using Dev C++ IDE.
Though we tried our best to keep our code clean. If you need any explanation of above code. Do not hesitate to contact us.