Untitled
unknown
plain_text
a year ago
2.1 kB
3
Indexable
Never
#include <iostream> #include <fstream> #include <cstring> using namespace std; struct StudentRecord { char name[20]; int rollNo; float marks; }; void insertRecord() { StudentRecord newRecord; ofstream outfile("student_records.txt", ios::app | ios::binary); cout << "Enter student name: "; cin >> newRecord.name; cout << "Enter student roll number: "; cin >> newRecord.rollNo; cout << "Enter student marks: "; cin >> newRecord.marks; outfile.write((char*)&newRecord, sizeof(newRecord)); outfile.close(); } void displayRecords() { StudentRecord currentRecord; ifstream infile("student_records.txt", ios::binary); while (infile.read((char*)¤tRecord, sizeof(currentRecord))) { cout << "Name: " << currentRecord.name << endl; cout << "Roll No: " << currentRecord.rollNo << endl; cout << "Marks: " << currentRecord.marks << endl << endl; } infile.close(); } void searchRecord() { int rollNo; StudentRecord currentRecord; ifstream infile("student_records.txt", ios::binary); cout << "Enter roll number to search: "; cin >> rollNo; while (infile.read((char*)¤tRecord, sizeof(currentRecord))) { if (currentRecord.rollNo == rollNo) { cout << "Name: " << currentRecord.name << endl; cout << "Roll No: " << currentRecord.rollNo << endl; cout << "Marks: " << currentRecord.marks << endl << endl; break; } } infile.close(); } int main() { int choice; do { cin >> choice; switch (choice) { case 1: insertRecord(); break; case 2: displayRecords(); break; case 3: searchRecord(); break; case 4: cout << "Exiting program...\n"; break; default: cout << "Invalid choice! Please try again.\n"; break; } } while (choice != 4); return 0; }