0% found this document useful (0 votes)
52 views7 pages

Object Oriented Programming Aman Gupta 15BEC1162

This document discusses object oriented programming concepts through an example program that reads, writes, updates, and backs up student records stored in a text file. It defines a student structure to store the data and includes functions to display a menu, read all records, read a specific record, write a new record, update a record, and create a backup of the data file. The main function uses a switch menu to call the appropriate functions based on the user's selection.

Uploaded by

Aman Gupta
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)
52 views7 pages

Object Oriented Programming Aman Gupta 15BEC1162

This document discusses object oriented programming concepts through an example program that reads, writes, updates, and backs up student records stored in a text file. It defines a student structure to store the data and includes functions to display a menu, read all records, read a specific record, write a new record, update a record, and create a backup of the data file. The main function uses a switch menu to call the appropriate functions based on the user's selection.

Uploaded by

Aman Gupta
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/ 7

Object Oriented Programming

Aman Gupta 15BEC1162

#include<fstream>

#include<iomanip>

#include<iostream>

using namespace std;

struct student

int eno;

char name[20];

float marks;

};

//function to read all the records from the file

void file_allread()

struct student stud;

ifstream stud_file;

stud_file.open("student.txt");

char ch;

int i=1;

cout<<"=====Read All Records===="<<endl;

while(!stud_file.eof())

stud_file.read((char *)&stud, sizeof(struct student));

if(!stud_file.eof())
{

cout<<"Record Number:"<<i<<endl;

cout<<"identity No:"<<stud.eno<<endl;

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

cout<<"marks"<<stud.marks<<endl;

i++;

} }

stud_file.close();

cout<<"====End====="<<endl;

//Function to enter a specific record number and to view that record only

void file_spread()

struct student stud;

ifstream stud_file;

stud_file.open("student.txt");

char ch;

int r;

cout<<"====Read specific Record"<<endl;

cout<<"Record Number to view the Record";

cin>>r;

r--;

stud_file.seekg((sizeof(struct student)*r), ios::beg);

stud_file.read((char*)&stud, sizeof(struct student));

if(!stud_file.eof())

cout<<"student No:"<<stud.eno<<endl;

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

cout<<"marks"<<stud.marks<<endl; }
stud_file.close();

cout<<"====End===="<<endl;

//Function to write a record to a file

void file_write()

{ struct student stud;

ofstream stud_file;

stud_file.open("student.txt",ios::app);

char ch;

cout<<"====Write Record===="<<endl;

cout<<"student No:";

cin>>stud.eno;

cout<<"student Name:";

cin>>stud.name;

cout<<"marks";

cin>>stud.marks;

if(!stud_file.write((char*)&stud, sizeof(struct student)))

cout<<"Error in writing one record"<<endl;

else

cout<<"One record has been inserted successfully"<<endl;

stud_file.close();

cout<<"====End===="<<endl;

//Function to write an update to file

void file_update()

struct student stud;

fstream stud_file;

cout<<"====Update Record===="<<endl;
stud_file.open("student.txt");

char ch;

int r;

cout<<"Record number to update the record";

cin>>r;

r--;

stud_file.seekp((sizeof(struct student)*r),ios::beg);

cout<<"student No:";

cin>>stud.eno;

cout<<"student Name:";

cin>>stud.name;

cout<<"marks";

cin>>stud.marks;

if(!stud_file.write((char*)&stud, sizeof(struct student)))

cout<<"Error in writing one record"<<endl;

else

cout<<"One record has been updated successfully"<<endl;

stud_file.close();

cout<<"====End====="<<endl;

//copy data into backup file

void file_copy()

struct student stud;

ifstream stud_file;

stud_file.open("student.txt");

ofstream stud_bak_file;
stud_bak_file.open("student_backup.txt");

char ch;

int i=0;

while(!stud_file.eof())

stud_file.read((char*)&stud, sizeof(struct student)); //read record from source file

if(!stud_file.eof())

stud_bak_file.write((char*)&stud,sizeof(struct student));//write record in backup file

i++;

stud_file.close();

stud_bak_file.close();

cout<<i<<"records copied into backup file.."<<endl;

cout<<"====End===="<<endl;

//function to display menu

int menu()

int opt;

cout<<"===Menu==="<<endl;

cout<<"1.Read All Records"<<endl;

cout<<"2.Read a specific Record"<<endl;

cout<<"3.Write a Record"<<endl;

cout<<"4.Update a Record"<<endl;

cout<<"5.Backup file"<<endl;

cout<<"6.Exit"<<endl;

cout<<"======"<<endl;
while(1)

{ cout<<"enter your option";

cin>>opt;

if(opt>=1 && opt<=6)

break;

return opt; }

main()

int opt;

char ch;

while(1)

{ opt=menu();

if(opt==6)

break;

if(opt==1)

file_allread();

if(opt==2)

file_spread();

if(opt==3)

file_write();

if(opt==4)

file_update();

if(opt==5)

file_copy();

cout<<"Good Bye!!!"<<endl;

You might also like