KEMBAR78
Sequential File | PDF | Computer Programming | Software Engineering
0% found this document useful (0 votes)
16 views3 pages

Sequential File

This document is a C++ program that manages student records, allowing users to write, read, search, and delete student information stored in a text file. It defines a 'student' structure and uses a vector to hold student records, while providing a menu-driven interface for user interaction. Key functionalities include adding new records, displaying all records, searching for a specific student by roll number, and deleting records.

Uploaded by

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

Sequential File

This document is a C++ program that manages student records, allowing users to write, read, search, and delete student information stored in a text file. It defines a 'student' structure and uses a vector to hold student records, while providing a menu-driven interface for user interaction. Key functionalities include adding new records, displaying all records, searching for a specific student by roll number, and deleting records.

Uploaded by

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

#include <bits/stdc++.

h>
#include <fstream>
using namespace std;

// Structure to store student details


struct student {
int roll;
string name, div, address;
};

// Global vector to store student records


vector<student> students;

// Function to write student data to the file


void Write() {
student s;
cout << "Enter the roll no of student: ";
cin >> s.roll;

cout << "Enter the name of student: ";


cin.ignore(); // Clear input buffer before getline
getline(cin, s.name);

cout << "Enter the division of student: ";


cin >> s.div;

cout << "Enter the address of student: ";


cin.ignore(); // Clear input buffer before getline
getline(cin, s.address);

ofstream file("students.txt", ios::app); // Append mode


file << s.roll << "\t" << s.name << "\t" << s.div << "\t" << s.address << "\n";
file.close();

cout << "Student record added successfully!\n";


}

// Function to read and display all student records


void Read() {
ifstream file("students.txt");
string temp;

cout << "\nRoll no\tName\tDiv\tAddress\n";


while (getline(file, temp)) {
cout << temp << endl;
}

file.close();
}

// Function to load student records from file into vector


void appendtovec() {
ifstream file("students.txt");
student s;
students.clear(); // Remove old records

while (file >> s.roll) {


file.ignore(); // Skip tab after roll number
getline(file, s.name, '\t');
getline(file, s.div, '\t');
getline(file, s.address);
students.push_back(s);
}

file.close();
}

// Function to search for a student by roll number


void search() {
int roll;
cout << "Enter roll number to search: ";
cin >> roll;

bool found = false;


for (const auto& s : students) {
if (s.roll == roll) {
cout << "Student found: " << s.roll << "\t" << s.name << "\t" << s.div
<< "\t" << s.address << endl;
found = true;
break;
}
}

if (!found) {
cout << "Student not found!" << endl;
}
}

// Function to delete a student record by roll number


void Delete() {
int roll;
cout << "Enter roll number to delete: ";
cin >> roll;

ofstream temp("temp.txt", ios::app);


bool found = false;

for (const auto& s : students) {


if (s.roll == roll) {
found = true;
continue; // Skip writing this record
}
temp << s.roll << "\t" << s.name << "\t" << s.div << "\t" << s.address <<
"\n";
}

temp.close();

if (!found) {
cout << "Student not found!" << endl;
remove("temp.txt");
} else {
remove("students.txt");
rename("temp.txt", "students.txt");
cout << "Student deleted successfully!" << endl;
}
}
// Main menu-driven function
int main() {
int choice = 0;

do {
cout << "\n1. Write to file";
cout << "\n2. Read from file";
cout << "\n3. Search in file";
cout << "\n4. Delete from file";
cout << "\n5. Exit session";

cout << "\n\nEnter your choice: ";


cin >> choice;

switch (choice) {
case 1:
Write();
break;
case 2:
Read();
break;
case 3:
appendtovec();
search();
break;
case 4:
appendtovec();
Delete();
break;
case 5:
cout << "\nExiting...\n";
break;
default:
cout << "Invalid choice! Please try again.\n";
break;
}

} while (choice != 5);

return 0;
}

You might also like