Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdio.h>
- #include <fstream>
- #include <iomanip>
- using namespace std;
- class SFH {
- private:
- struct studentData {
- int roll;
- char name[20];
- } s;
- public:
- void create();
- void display();
- void search(int);
- void removeRecord(int);
- };
- void SFH::create() {
- fstream outfile;
- outfile.open("student.data", ios::binary | ios::out);
- int records;
- cout << "Enter the number of records: "; cin >> records;
- while (records--) {
- cout << "Enter Student Roll: "; cin >> s.roll;
- cout << "Enter Student Name: "; cin >> s.name;
- outfile.write((char *)&s, sizeof(s));
- }
- cout << endl;
- outfile.close();
- }
- void SFH::display() {
- fstream infile;
- infile.open("student.data", ios::binary | ios::in);
- cout << "\nDisplaying data...\n" << endl;
- cout << "Roll" << "\t" << "Name" << endl;
- cout << "-------------------------------" << endl;
- while (infile.read((char *)&s, sizeof(s))) {
- cout << right << setw(3) << s.roll << "\t" << s.name << endl;
- }
- cout << endl;
- infile.close();
- }
- void SFH::search(int key) {
- fstream infile;
- infile.open("student.data", ios::binary | ios::in);
- while (infile.read((char *)&s, sizeof(s))) {
- if (s.roll == key) {
- cout << "\nRecord Found!\n" << endl;
- cout << "Roll" << "\t" << "Name" << endl;
- cout << "-------------------------------" << endl;
- cout << right << setw(3) << s.roll << "\t" << s.name << endl;
- cout << endl;
- infile.close();
- return;
- }
- }
- cout << "\nRecord Not Found!\n" << endl;
- cout << endl;
- infile.close();
- }
- void SFH::removeRecord(int key) {
- // TODO - Exception if record does not exist
- fstream tempfile, infile;
- infile.open("student.data", ios::binary | ios::in);
- tempfile.open("temp.data", ios::binary | ios::out);
- bool exists = false;
- while (infile.read(((char *)&s), sizeof(s))) {
- if (s.roll == key) {
- exists = true;
- break;
- }
- }
- if (exists) {
- while (infile.read(((char *)&s), sizeof(s))) {
- if (s.roll == key) continue;
- else {
- tempfile.write((char *)&s, sizeof(s));
- }
- }
- infile.close();
- tempfile.close();
- remove("student.data");
- rename("temp.data", "student.data");
- cout << "\nRecord Deleted Successfully\n" << endl;
- } else {
- infile.close();
- tempfile.close();
- cout << "\nRecord does not exist!\n" << endl;
- }
- };
- int main() {
- SFH obj;
- int choice;
- while (true) {
- cout << "----- MENU -----" << endl;
- cout << "1. Create" << endl;
- cout << "2. Display" << endl;
- cout << "3. Search" << endl;
- cout << "4. Delete" << endl;
- cout << "5. Exit" << endl;
- cout << "----------------" << endl;
- cout << "Enter your choice: "; cin >> choice;
- if (choice == 1) {
- obj.create();
- } else if (choice == 2) {
- obj.display();
- } else if (choice == 3) {
- int key;
- cout << "Enter Roll No. to be searched: "; cin >> key;
- obj.search(key);
- } else if (choice == 4) {
- int key;
- cout << "Enter the Roll No. to be deleted: "; cin >> key;
- obj.removeRecord(key);
- } else if (choice == 5) {
- cout << "Terminating Program..." << endl;
- break;
- } else {
- cout << "Invalid Choice" << endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment