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

File Handling

The document is a C++ program that manages student records using a binary file. It allows users to write, display, search, delete, and modify student records based on admission numbers. The program utilizes a class to encapsulate student data and provides a menu-driven interface for user interaction.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

File Handling

The document is a C++ program that manages student records using a binary file. It allows users to write, display, search, delete, and modify student records based on admission numbers. The program utilizes a class to encapsulate student data and provides a menu-driven interface for user interaction.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include<iostream>

#include<fstream>
#include<cstdio>
using namespace std;

class Student
{
int admno;
char name[50];
char addr[50];
public:
void setData()
{
cout << "\nEnter Roll no. ";
cin >> admno;
cout << "Enter name of student ";
//cin.getline(name,50);
cin>>name;
cout<<"enter the address of the student";
cin>>addr;

void showData()
{
cout << "\nAdmission no. : " << admno;
cout << "\nStudent Name : " << name;
cout<<"\nAddress:"<<addr;
}

int retAdmno()
{
return admno;
}
};

/*
* function to write in a binary file.
*/

void write_record()
{
ofstream outFile;
outFile.open("student.dat", ios::binary | ios::app);

Student obj;
obj.setData();

outFile.write((char*)&obj, sizeof(obj));

outFile.close();
}

/*
* function to display records of file
*/

void display()
{
ifstream inFile;
inFile.open("student.dat", ios::binary);

Student obj;

while(inFile.read((char*)&obj, sizeof(obj)))
{
obj.showData();
}

inFile.close();
}

/*
* function to search and display from binary file
*/

void search(int n)
{
ifstream inFile;
inFile.open("student.dat", ios::binary);

Student obj;

while(inFile.read((char*)&obj, sizeof(obj)))
{
if(obj.retAdmno() == n)
{
obj.showData();
break;
}
}

inFile.close();
}

/*
* function to delete a record
*/

void delete_record(int n)
{
Student obj;
ifstream inFile;
inFile.open("student.dat", ios::binary);

ofstream outFile;
outFile.open("temp.dat", ios::out | ios::binary);

while(inFile.read((char*)&obj, sizeof(obj)))
{
if(obj.retAdmno() != n)
{
outFile.write((char*)&obj, sizeof(obj));
}
}

inFile.close();
outFile.close();

remove("student.dat");
rename("temp.dat", "student.dat");
}

/*
* function to modify a record
*/

void modify_record(int n)
{
fstream file;
file.open("student.dat",ios::in | ios::out);

Student obj;

while(file.read((char*)&obj, sizeof(obj)))
{
if(obj.retAdmno() == n)
{
cout << "\nEnter the new details of student";
obj.setData();

int pos = -1 * sizeof(obj);


file.seekp(pos, ios::cur);

file.write((char*)&obj, sizeof(obj));
}
}

file.close();
}

int main()
{
int ch;
do{
cout<<"\nMenu\n\t1.write\n2.display\n3.search\n4.delete\n5.modify";
cout<<"\nEnter your choice";
cin>>ch;
switch(ch){

case 1: cout<<"Enter number of records";//Store 4 records in file


int n;
cin>>n;
for(int i = 0; i <n; i++)
write_record();
break;
case 2:
//Display all records
cout << "\nList of records";
display();
break;

case 3://Search record


cout<<"Enter admission no. to be searched";
int s;
cin>>s;
search(s);
break;
case 4:
cout<<"enter no to be deleted";
int d;
cin>>d;
//Delete record
delete_record(d);
cout << "\nRecord Deleted";
break;
case 5:

//Modify record
cout<<"enter rno to be modified";
int m;
cin>>m;

modify_record(m);
break;
case 6:

return 0;
}
}while(ch!=6);
}

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