sidrs

DSAL_AS - Sequential File Handling

Apr 6th, 2025 (edited)
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.15 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <fstream>
  4. #include <iomanip>
  5. using namespace std;
  6.  
  7.  
  8. class SFH {
  9. private:
  10.     struct studentData {
  11.         int roll;
  12.         char name[20];
  13.     } s;
  14. public:
  15.     void create();
  16.     void display();
  17.     void search(int);
  18.     void removeRecord(int);
  19. };
  20.  
  21. void SFH::create() {
  22.     fstream outfile;
  23.     outfile.open("student.data", ios::binary | ios::out);
  24.     int records;
  25.     cout << "Enter the number of records: "; cin >> records;
  26.     while (records--) {
  27.         cout << "Enter Student Roll: "; cin >> s.roll;
  28.         cout << "Enter Student Name: "; cin >> s.name;
  29.         outfile.write((char *)&s, sizeof(s));
  30.     }
  31.     cout << endl;
  32.     outfile.close();
  33. }
  34.  
  35. void SFH::display() {
  36.     fstream infile;
  37.     infile.open("student.data", ios::binary | ios::in);
  38.     cout << "\nDisplaying data...\n" << endl;
  39.     cout << "Roll" << "\t" << "Name" << endl;
  40.     cout << "-------------------------------" << endl;
  41.     while (infile.read((char *)&s, sizeof(s))) {
  42.         cout << right << setw(3) << s.roll << "\t" << s.name << endl;
  43.     }
  44.     cout << endl;
  45.     infile.close();
  46. }
  47.  
  48. void SFH::search(int key) {
  49.     fstream infile;
  50.     infile.open("student.data", ios::binary | ios::in);
  51.     while (infile.read((char *)&s, sizeof(s))) {
  52.         if (s.roll == key) {
  53.             cout << "\nRecord Found!\n" << endl;
  54.             cout << "Roll" << "\t" << "Name" << endl;
  55.             cout << "-------------------------------" << endl;
  56.             cout << right << setw(3) << s.roll << "\t" << s.name << endl;
  57.             cout << endl;
  58.             infile.close();
  59.             return;
  60.         }
  61.     }
  62.     cout << "\nRecord Not Found!\n" << endl;
  63.     cout << endl;
  64.     infile.close();
  65. }
  66.  
  67. void SFH::removeRecord(int key) {
  68.     // TODO - Exception if record does not exist
  69.     fstream tempfile, infile;
  70.     infile.open("student.data", ios::binary | ios::in);
  71.     tempfile.open("temp.data", ios::binary | ios::out);
  72.  
  73.     bool exists = false;
  74.     while (infile.read(((char *)&s), sizeof(s))) {
  75.         if (s.roll == key) {
  76.             exists = true;
  77.             break;
  78.         }
  79.     }
  80.    
  81.     if (exists) {
  82.         while (infile.read(((char *)&s), sizeof(s))) {
  83.             if (s.roll == key) continue;
  84.             else {
  85.                 tempfile.write((char *)&s, sizeof(s));
  86.             }
  87.         }
  88.         infile.close();
  89.         tempfile.close();
  90.         remove("student.data");
  91.         rename("temp.data", "student.data");
  92.         cout << "\nRecord Deleted Successfully\n" << endl;
  93.     } else {
  94.         infile.close();
  95.         tempfile.close();
  96.         cout << "\nRecord does not exist!\n" << endl;
  97.     }
  98.  
  99.  
  100. };
  101.  
  102.  
  103. int main() {
  104.     SFH obj;
  105.     int choice;
  106.     while (true) {
  107.         cout << "----- MENU -----" << endl;
  108.         cout << "1. Create" << endl;
  109.         cout << "2. Display" << endl;
  110.         cout << "3. Search" << endl;
  111.         cout << "4. Delete" << endl;
  112.         cout << "5. Exit" << endl;
  113.         cout << "----------------" << endl;
  114.         cout << "Enter your choice: "; cin >> choice;
  115.  
  116.         if (choice == 1) {
  117.             obj.create();
  118.         } else if (choice == 2) {
  119.             obj.display();
  120.         } else if (choice == 3) {
  121.             int key;
  122.             cout << "Enter Roll No. to be searched: "; cin >> key;
  123.             obj.search(key);
  124.         } else if (choice == 4) {
  125.             int key;
  126.             cout << "Enter the Roll No. to be deleted: "; cin >> key;
  127.             obj.removeRecord(key);
  128.         } else if (choice == 5) {
  129.             cout << "Terminating Program..." << endl;
  130.             break;
  131.         } else {
  132.             cout << "Invalid Choice" << endl;  
  133.         }
  134.  
  135.     }
  136.     return 0;
  137. }
  138.  
Advertisement
Add Comment
Please, Sign In to add comment