Skip to content

Instantly share code, notes, and snippets.

@shivajichalise
Last active July 26, 2021 07:27
Show Gist options
  • Save shivajichalise/67d999e39256b45dd1dddadb6f431a11 to your computer and use it in GitHub Desktop.
Save shivajichalise/67d999e39256b45dd1dddadb6f431a11 to your computer and use it in GitHub Desktop.
C++ File handling && CRUD program
/*NOTE: IAM NOT A C++ PRO and THIS CODE CAN BE IMPROVED BY ALOT*/
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cstdio>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
class Person{
protected:
char name[20];
int age;
int ID;
public:
void getRecords(void){
cout <<"Enter ID: "; cin>>ID;
fflush(stdin);
cout<<"Enter name: "; cin >> name;
cout<<"Enter age: "; cin >> age;
}
int getID(){
return ID;
}
};
class Student: public Person {
private:
int rollno;
public:
void getData(){
cout << "Enter student's rollno: "; cin>>rollno;
}
void showRecords(void){
cout << setw(5) << ID << setw(15) << name << setw(15) << setw(15) << age << setw(15) << rollno <<endl;
}
};
class Employee: public Person {
private:
int salary;
public:
void getData(){
cout << "Enter employee's salary: "; cin>>salary;
}
void showRecords(void){
cout << setw(5) << ID << setw(15) << name << setw(15) << setw(15) << age << setw(15) << salary <<endl;
}
};
void addRecord(Student& s, Employee& e , fstream& file, char f){
if(f == 's'){
file.open("studentRecords.dat", ios::app | ios::binary);
s.getRecords();
s.getData();
file.write((char*)&s, sizeof(s));
} else {
file.open("employeeRecords.dat", ios::app | ios::binary);
e.getRecords();
e.getData();
file.write((char*)&e, sizeof(e));
}
file.close();
}
void showAllRecords(Student& s, Employee& e, fstream& file, char f){
if(f=='s'){
file.open("studentRecords.dat", ios::in | ios::binary);
if(!file){
cout << "File not found";
exit(0);
}else{
file.read((char*)&s, sizeof(s));
while(!file.eof()){
s.showRecords();
file.read((char*)&s, sizeof(s));
}
}
}else{
file.open("employeeRecords.dat", ios::in | ios::binary);
if(!file){
cout << "File not found";
exit(0);
}else{
file.read((char*)&e, sizeof(e));
while(!file.eof()){
e.showRecords();
file.read((char*)&e, sizeof(e));
}
}
}
file.close();
}
void deleteRecord(Student& s, Employee& e, fstream& file, char f){
int id;
cout << "Enter ID to delete: "; cin >> id;
ofstream studentRecords2, employeeRecords2;
if(f=='s'){
studentRecords2.open("newStudent.dat", ios::out | ios::binary);
file.open("studentRecords.dat", ios::in | ios::binary);
if(!file){
cout << "File not found!";
exit(0);
}else{
file.read((char*)&s, sizeof(s));
while(!file.eof()){
if(id != s.getID()){
studentRecords2.write((char*)&s, sizeof(s));
}
file.read((char*)&s, sizeof(s));
}
studentRecords2.close();
file.close();
remove("studentRecords.dat");
rename("newStudent.dat", "studentRecords.dat");
}
}else{
employeeRecords2.open("newEmployee.dat", ios::out | ios::binary);
file.open("employeeRecords.dat", ios::in | ios::binary);
if(!file){
cout << "File not found!";
exit(0);
}else{
file.read((char*)&e, sizeof(e));
while(!file.eof()){
if(id != e.getID()){
employeeRecords2.write((char*)&e, sizeof(e));
}
file.read((char*)&e, sizeof(e));
}
employeeRecords2.close();
file.close();
remove("employeeRecords.dat");
rename("newEmployee.dat", "employeeRecords.dat");
}
}
}
void editRecord(Student& s, Employee& e, fstream& file, char f) {
ofstream studentRecordsE, employeeRecordsE;
int id;
cout << "Enter the ID you want to edit: ";
cin >> id;
if (f == 's') {
studentRecordsE.open("studentRecordsE.dat", ios::out | ios::binary);
file.open("studentRecords.dat", ios::in | ios::out | ios::binary);
if(!file){
cout << "File not found!";
exit(0);
}
file.read((char*)&s, sizeof(s));
while (!file.eof()) {
if(id != s.getID()){
studentRecordsE.write((char*)&s, sizeof(s));
}else if(id == s.getID()){
s.getRecords();
s.getData();
studentRecordsE.write((char*)&s, sizeof(s));
}
file.read((char*)&s, sizeof(s));
}
studentRecordsE.close();
file.close();
remove("studentRecords.dat");
rename("studentRecordsE.dat", "studentRecords.dat");
} else {
employeeRecordsE.open("employeeRecordsE.dat", ios::out | ios::binary);
file.open("employeeRecords.dat", ios::in | ios::out | ios::binary);
if(!file){
cout << "File not found!";
exit(0);
}
file.read((char*)&e, sizeof(e));
while (!file.eof()) {
if(id != e.getID()){
employeeRecordsE.write((char*)&e, sizeof(e));
}else if(id == e.getID()){
e.getRecords();
e.getData();
employeeRecordsE.write((char*)&e, sizeof(e));
}
file.read((char*)&e, sizeof(e));
}
employeeRecordsE.close();
file.close();
remove("employeeRecords.dat");
rename("employeeRecordsE.dat", "employeeRecords.dat");
}
}
Student student1;
Employee employee1;
fstream studentFile, employeeFile;
int main(){
int option;
int option2;
while(1){
/* system("clear"); */
cout<<"*******************************"<<endl;
cout<<"[1] Add Record"<<endl;
cout<<"[2] View All Record"<<endl;
cout<<"[3] Delete Record"<<endl;
cout<<"[4] Edit Record"<<endl;
cout << "Enter your choice and press return: "; cin >> option;
switch(option){
case 1:
cout<<"[1] Add Student record"<<endl;
cout<<"[2] Add Employee record"<<endl;
cout<< "Enter your choice and press return: "; cin >> option2;
switch(option2){
case 1:
addRecord(student1, employee1, studentFile, 's');
cout << "Press any key";
break;
case 2:
addRecord(student1, employee1, employeeFile, 'e');
cout << "Press any key";
break;
default:
cout << "Incorrect Choice!" << endl;
}
break;
case 2:
cout<<"[1] View Student record"<<endl;
cout<<"[2] View Employee record"<<endl;
cout<< "Enter your choice and press return: "; cin >> option2;
switch(option2){
case 1:
cout << "------------------------------------------------------------------------------------------" << endl;
cout << setw(5) << "ID" << setw(15) << "Name" << setw(15) << setw(15) << "Age" << setw(15) << "Rollno" << endl;
cout << "------------------------------------------------------------------------------------------" << endl;
showAllRecords(student1, employee1, studentFile, 's');
cout << "------------------------------------------------------------------------------------------" << endl;
break;
case 2:
cout << "------------------------------------------------------------------------------------------" << endl;
cout << setw(5) << "ID" << setw(15) << "Name" << setw(15) << setw(15) << "Age" << setw(15) << "Salary" << endl;
cout << "------------------------------------------------------------------------------------------" << endl;
showAllRecords(student1, employee1, employeeFile, 'e');
cout << "------------------------------------------------------------------------------------------" << endl;
break;
default:
cout << "Incorrect Choice!" << endl;
}
break;
case 3:
cout<<"[1] Delete Student record"<<endl;
cout<<"[2] Delete Employee record"<<endl;
cout<< "Enter your choice and press return: "; cin >> option2;
switch(option2){
case 1:
deleteRecord(student1, employee1, studentFile, 's');;
break;
case 2:
deleteRecord(student1, employee1, employeeFile, 'e');
break;
default:
cout << "Incorrect Choice!" << endl;
}
break;
case 4:
cout<<"[1] Modify Student record"<<endl;
cout<<"[2] Modify Employee record"<<endl;
cout<< "Enter your choice and press return: "; cin >> option2;
switch(option2){
case 1:
editRecord(student1, employee1, studentFile, 's');
break;
case 2:
editRecord(student1, employee1, employeeFile, 'e');
break;
default:
cout << "Incorrect Choice!" << endl;
}
break;
case 5:
exit(0);
break;
default:
cout << "Incorrect Choice!" << endl;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment