0% found this document useful (0 votes)
25 views4 pages

Practical 11 (DSA)

The document outlines a C++ program for a Student Information System that allows users to add, delete, and display student records. It defines a Student structure and includes functions for managing student data in a binary file. The main function provides a menu for user interaction with options to add, delete, display students, or exit the program.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views4 pages

Practical 11 (DSA)

The document outlines a C++ program for a Student Information System that allows users to add, delete, and display student records. It defines a Student structure and includes functions for managing student data in a binary file. The main function provides a menu for user interaction with options to add, delete, display students, or exit the program.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Lab Assignment 11

Code:
#include <iostream> getline(cin, student.name);
#include <fstream>
#include <string> cout << "Enter Division: ";
using namespace std; getline(cin, student.division);

// Student structure cout << "Enter Address: ";


struct Student { getline(cin, student.address);
int rollNumber;
string name; // Write the student information to the file
string division;
file.write(reinterpret_cast<char*>(&student),
string address; sizeof(student));
};

cout << "Student added successfully!" <<


// Function to add a student record endl;

void addStudent(const string &filename) {


ofstream file; file.close();

file.open(filename, ios::app); // Open file in }


append mode

// Function to delete a student record


if (!file) { void deleteStudent(const string &filename) {
cerr << "Error opening file!" << endl; ifstream file(filename, ios::binary);
return; ofstream tempFile("temp.dat", ios::binary);
}

if (!file || !tempFile) {
Student student; cerr << "Error opening file!" << endl;
cout << "Enter Roll Number: "; return;
cin >> student.rollNumber; }
cin.ignore(); // Ignore newline character left
by cin
int rollNumber;
cout << "Enter Roll Number of student to
cout << "Enter Name: "; delete: ";
cin >> rollNumber; ifstream file(filename, ios::binary);

bool studentFound = false; if (!file) {


Student student; cerr << "Error opening file!" << endl;
return;
while }
(file.read(reinterpret_cast<char*>(&student),
sizeof(student))) {
if (student.rollNumber != rollNumber) { int rollNumber;
cout << "Enter Roll Number of student to
tempFile.write(reinterpret_cast<char*>(&stud display: ";
ent), sizeof(student)); cin >> rollNumber;
} else {
studentFound = true; bool studentFound = false;
} Student student;
}

while
if (studentFound) { (file.read(reinterpret_cast<char*>(&student),
sizeof(student))) {
cout << "Student record deleted
successfully!" << endl; if (student.rollNumber == rollNumber) {

} else { cout << "Student Found!" << endl;

cout << "Student with Roll Number " << cout << "Roll Number: " <<
rollNumber << " not found!" << endl; student.rollNumber << endl;

} cout << "Name: " << student.name <<


endl;
cout << "Division: " <<
file.close(); student.division << endl;
tempFile.close(); cout << "Address: " << student.address
<< endl;
studentFound = true;
// Remove original file and rename the temp
file break;
remove(filename.c_str()); }
rename("temp.dat", filename.c_str()); }
}
if (!studentFound) {
// Function to display a student record cout << "Student with Roll Number " <<
rollNumber << " not found!" << endl;
void displayStudent(const string &filename) {
}
addStudent(filename);
file.close(); break;
} case 2:
deleteStudent(filename);
int main() { break;
string filename = "students.dat"; // File to case 3:
store student records
displayStudent(filename);
int choice;
break;
case 4:
do {
cout << "Exiting program..." <<
cout << "\n--- Student Information endl;
System ---\n";
break;
cout << "1. Add Student\n";
default:
cout << "2. Delete Student\n";
cout << "Invalid choice! Please try
cout << "3. Display Student\n"; again." << endl;
cout << "4. Exit\n"; }
cout << "Enter your choice: "; } while (choice != 4);
cin >> choice;
return 0;
switch (choice) { }
case 1:
Output:

--- Student Information System ---


1. Add Student
2. Delete Student
3. Display Student
4. Exit
Enter your choice: 1

You might also like