0% found this document useful (0 votes)
15 views6 pages

Ds Practical 10

The document outlines a C++ program for managing student information, allowing users to add, delete, and display student records stored in a sequential file. It includes functions for adding a student, deleting a student by roll number, and displaying a student's details if found. The program features a menu-driven interface for user interaction and error handling for file operations.

Uploaded by

karan.ppjgk22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views6 pages

Ds Practical 10

The document outlines a C++ program for managing student information, allowing users to add, delete, and display student records stored in a sequential file. It includes functions for adding a student, deleting a student by roll number, and displaying a student's details if found. The program features a menu-driven interface for user interaction and error handling for file operations.

Uploaded by

karan.ppjgk22
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Practical No.

10

Q. Department maintains a student information. The file contains roll number,


name, division and address. Allow user to add, delete information of student.
Display information of particular employee. If record of student does not exist
an appropriate message is displayed. If it is, then the system displays the
student details. Use sequential file to maintain the data.

#include <iostream>

#include <fstream>

#include <cstring>

using namespace std;

struct Student {

int rollNumber;

char name[50];

char division[10];

char address[100];

};

void addStudent(const char* filename) {

fstream file;

Student student;

file.open(filename, ios::out | ios::app);

if (!file) {

cout << "Error in opening file for writing!" << endl;

return;

}p

cout << "Enter Roll Number: ";

cin >> student.rollNumber;

cin.ignore();
cout << "Enter Name: ";

cin.getline(student.name, 50);

cout << "Enter Division: ";

cin.getline(student.division, 10);

cout << "Enter Address: ";

cin.getline(student.address, 100);

file.write(reinterpret_cast<char*>(&student), sizeof(student));

cout << "Student record added successfully!" << endl;

file.close();

void deleteStudent(const char* filename, int rollNumber) {

fstream file, tempFile;

Student student;

bool found = false;

file.open(filename, ios::in);

if (!file) {

cout << "Error in opening file for reading!" << endl;

return;

tempFile.open("temp.dat", ios::out);

if (!tempFile) {

cout << "Error in creating temporary file!" << endl;

file.close();

return;

while (file.read(reinterpret_cast<char*>(&student), sizeof(student))) {


if (student.rollNumber == rollNumber) {

found = true;

continue;

tempFile.write(reinterpret_cast<char*>(&student), sizeof(student));

if (found) {

cout << "Student record deleted successfully!" << endl;

} else {

cout << "No student found with Roll Number: " << rollNumber << endl;

file.close();

tempFile.close();

remove(filename);

rename("temp.dat", filename);

void displayStudent(const char* filename, int rollNumber) {

fstream file;

Student student;

bool found = false;

file.open(filename, ios::in);

if (!file) {

cout << "Error in opening file for reading!" << endl;

return;

while (file.read(reinterpret_cast<char*>(&student), sizeof(student))) {


if (student.rollNumber == rollNumber) {

found = true;

cout << "Student Found!" << endl;

cout << "Roll Number: " << student.rollNumber << endl;

cout << "Name: " << student.name << endl;

cout << "Division: " << student.division << endl;

cout << "Address: " << student.address << endl;

break;

if (!found) {

cout << "No student found with Roll Number: " << rollNumber << endl;

file.close();

void menu() {

const char* filename = "students.dat";

int choice, rollNumber;

do {

cout << "\n--- Student Information System ---\n";

cout << "1. Add Student\n";

cout << "2. Delete Student\n";

cout << "3. Display Student\n";

cout << "4. Exit\n";

cout << "Enter your choice: ";

cin >> choice;


switch (choice) {

case 1:

addStudent(filename);

break;

case 2:

cout << "Enter Roll Number of student to delete: ";

cin >> rollNumber;

deleteStudent(filename, rollNumber);

break;

case 3:

cout << "Enter Roll Number of student to display: ";

cin >> rollNumber;

displayStudent(filename, rollNumber);

break;

case 4:

cout << "Exiting the system.\n";

break;

default:

cout << "Invalid choice. Please try again.\n";

} while (choice != 4);

int main() {

menu();

return 0;

}Output:-

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy