0% found this document useful (0 votes)
13 views

Assignment 10

assignment 10

Uploaded by

wtf213
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Assignment 10

assignment 10

Uploaded by

wtf213
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Roll number : 23216

#include <iostream>

#include <fstream>

using namespace std;

class FileHandling{

public:

//student data

int records;

//getting input from user

void getInput(){

string line;

ofstream MyFile("StudentData.txt");

if(MyFile){

cout<<"\nEnter number of records: ";

cin>>records;

cin.ignore();

for(int i=0;i<records;i++){

cout<<"Student "<<i+1<<endl;

cout<<"Enter RollNumber, Name, Divison, Address, Date of birth, Percentage,


Grade"<<endl;

getline(cin,line);

MyFile<<line<<endl;

MyFile.close();

else cout<<"\nError opening file"<<endl;

}
//display student data

void displayData(){

string line;

cout<<endl;

ifstream fin("StudentData.txt");

if(fin){

while(getline(fin,line)){

cout << line << endl;

fin.close();

else cout<<"\nFile not open!"<<endl;

//appending student data

void appendData(){

string line;

cout<<"\nAdd Record: ";

ofstream fapp("StudentData.txt",ios::app);

if(fapp){

cout<<"Student "<<++records<<endl;

cout<<"Enter RollNumber, Name, Divison, Address, Date of birth, Percentage, Grade"<<endl;

getline(cin,line);

fapp<<line<<endl;

fapp.close();

else{

cout<<"Unable to open file"<<endl;

}
//search for student in database

void searchStudent(){

string line;

cout<<"\nSearch for record"<<endl;

ifstream fin("StudentData.txt");

if(fin){

string rollNo;

cout<<"Enter roll number: ";

cin>>rollNo;

cin.ignore();

bool notfound=true;

while(getline(fin,line)){

if(line.find(rollNo)!=string::npos){

cout<< line << endl;

fin.close();

notfound=false;

break;

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

else cout<<"Unable to open file!"<<endl;

//modify student record in database

void modifyRecord(){

ifstream Database("StudentData.txt");

if(Database){

ofstream temp("TempFile.txt");

if(temp){
string line,rollno;

cout<<"\nEnter student roll no to modify: ";

cin>>rollno;

cin.ignore();

bool notfound=true;

while(getline(Database,line)){

if(line.find(rollno)!=string::npos && notfound){

cout<<"Current Student Record: "<<line<<endl;

cout<<"Enter new record: ";

getline(cin,line);

temp<<line<<endl;

notfound=false;

else{

temp<<line<<endl;

if(notfound) cout<<"Student not found"<<endl;

temp.close();

else{

cout<<"Unable to open temporary file"<<endl;

Database.close();

remove("StudentData.txt");

rename("TempFile.txt","StudentData.txt");

else{

cout<<"Unable to open Database!"<<endl;

}
//delete student record in database

void deleteRecord(){

ifstream Database("StudentData.txt");

if(Database){

ofstream temp("TempFile.txt");

if(temp){

string line,rollno;

cout<<"\nEnter student roll no to delete: ";

cin>>rollno;

cin.ignore();

bool notfound=true;

while(getline(Database,line)){

if(line.find(rollno)!=string::npos && notfound){

notfound=false;

else{

temp<<line<<endl;

if(notfound) cout<<"Student not found"<<endl;

temp.close();

else{

cout<<"Unable to open temporary file"<<endl;

Database.close();

remove("StudentData.txt");rename("TempFile.txt","StudentData.txt");

else cout<<"Unable to open database"<<endl;

}
};

//driver function

int main(){

FileHandling f;

int choice=0;

while(choice<=6 && choice>=0){

cout<<"\nSTUDENT DATABASE\n1.INPUT RECORDS\n2.DISPLAY RECORDS\n3.SEARCH FOR A


RECORD\n4.MODIFY A RECORD\n5.DELETE A RECORD\n6.APPEND RECORD\n7.EXIT\nENTER YOUR
CHOICE: ";

cin>>choice;

cin.ignore();

switch(choice){

case 1:

f.getInput();

break;

case 2:

f.displayData();

break;

case 3:

f.searchStudent();

break;

case 4:

f.modifyRecord();

break;

case 5:

f.deleteRecord();

break;

case 6:

f.appendData();

break;

default:
cout<<"Exiting"<<endl;

break;

You might also like