#include <iostream>
#include <fstream>
#include <cstring>
#include <iomanip>
using namespace std;
const int MAX = 20;
class Student
{
int rollno;
char name[20], city[30];
int div;
int year;
public:
Student()
{
strcpy(name, "");
strcpy(city, "");
rollno = year = div = 0;
}
Student(int rollno, char name[MAX], int year, int div, char city[MAX])
{
strcpy(this->name, name);
strcpy(this->city, city);
this->rollno = rollno;
this->year = year;
this->div = div;
}
int getRollNo()
{
return rollno;
}
void displayRecord()
{
cout << endl
<< setw(5) << rollno << setw(10) << name << setw(10)
<< year << setw(10) << div << setw(15) << city;
}
};
//==========File Operations ===========
class FileOperations
{
fstream file;
public:
FileOperations(char *filename)
{
file.open(filename, ios::in | ios::out | ios::ate | ios::binary);
}
void insertRecord(int rollno, char name[MAX], int year, int div,
char city[MAX])
{
Student s1(rollno, name, year, div, city);
file.seekp(0, ios::end);
file.write((char *)&s1, sizeof(s1));
file.clear();
}
void displayAll()
{
Student s1;
file.seekg(0, ios::beg);
while (file.read((char *)&s1, sizeof(s1)))
{
s1.displayRecord();
}
cout << endl;
file.clear();
}
void searchRecord(int rollNo)
{
Student s1;
file.seekg(0, ios::beg);
bool flag = false;
while (file.read((char *)&s1, sizeof(s1)))
{
if (s1.getRollNo() == rollNo)
{
s1.displayRecord();
flag = true;
break;
}
}
cout << endl;
if (flag == false)
{
cout << "\nRecord of " << rollNo << "is not present." << endl;
}
file.clear();
}
void deleteRecord(int rollno)
{
ofstream outFile("new.txt", ios::binary);
file.seekg(0, ios::beg);
bool flag = false;
Student s1;
while (file.read((char *)&s1, sizeof(s1)))
{
if (s1.getRollNo() == rollno)
{
flag = true;
continue;
}
outFile.write((char *)&s1, sizeof(s1));
}
if (!flag)
{
cout << "\nRecord of " << rollno << " is not present.";
}
file.close();
outFile.close();
remove("student.txt");
rename("new.txt", "student.txt");
file.open("student.txt", ios::in | ios::out | ios::ate | ios::binary);
cout << "Record Deleted Successfully!!!" << endl;
}
~FileOperations()
{
file.close();
cout << "File Closed.";
}
};
int main()
{
ofstream newFile("student.txt", ios::app | ios::binary);
newFile.close();
FileOperations file((char *)"student.txt");
int rollNo, year, choice, div;
char name[MAX], address[MAX];
char cont = 'y';
while (cont == 'y')
{
cout << "<--------Menu-------->" << endl;
cout << "1. Add New Records" << endl;
cout << "2. Display All Records" << endl;
cout << "3. Search the Record" << endl;
cout << "4. Delete a Record" << endl;
cout << "5. Exit" << endl;
;
cout << "Enter choice : ";
cin >> choice;
switch (choice)
{
case 1:
int n;
cout << "Enter the number of records: ";
cin >> n;
for (int i = 0; i < n; i++)
{
cout << "\n -----------------------------------------\n";
cout << "Enter the details of student: " << endl;
cout << "Name: ";
cin >> name;
cout << "Division: ";
cin >> div;
cout << "Roll No. : ";
cin >> rollNo;
cout << "Year: ";
cin >> year;
cout << "Address: ";
cin >> address;
file.insertRecord(rollNo, name, year, div, address);
}
cout << "Records Inserted Successfully!!!" << endl;
break;
case 2:
cout << endl
<< setw(5) << "ROLL" << setw(10) << "NAME" << setw(10)
<< "YEAR" << setw(10) << "DIV" << setw(15) << "CITY";
file.displayAll();
break;
case 3:
cout << "Enter Roll no. to be Searched: ";
cin >> rollNo;
cout << endl
<< setw(5) << "ROLL" << setw(10) << "NAME" << setw(10)
<< "YEAR" << setw(10) << "DIV" << setw(15) << "CITY";
file.searchRecord(rollNo);
break;
case 4:
cout << "Enter Roll no. to be deleted: ";
cin >> rollNo;
file.deleteRecord(rollNo);
break;
default:
cout << "Invalid Choice!!!" << endl;
break;
}
cout << "Do you want to continue ? (y/n) : ";
cin >> cont;
}
cout << "Program Ended Successfully!!!" << endl;
return 0;
}
Comments
Post a Comment