#include<iostream>
#include<fstream>
using namespace std;
class student
{
int roll_no;
string name,div,address;
public:
student();
void add_record();
void delete_data(string);
void modify_record();
void search_data(string);
void input_data();
void write_data();
void read_data();
};
student :: student() : roll_no(0) , name(""), address(""),div (""){}
void student :: input_data()
{
cout<<": ENTER STUDENTS DETAILS :"<<endl<<endl;
cout<<": ROLL NO = ";
cin>>roll_no;
cin.ignore();
cout<<": NAME = ";
getline(cin,name);
cout<<": DIVISION = ";
getline(cin , div);
cout<<": ADDRESS = ";
getline(cin , address);
write_data();
}
// FUNCTION TO WRITE DATA INTO THE FILE
void student :: write_data()
{
fstream obj;
obj.open("student_data.txt" , ios::out | ios::app);
obj << roll_no <<" "<<name<<" "<<div<<" "<<address<<endl;
cout<<endl<<"FILE UPDATED SUCCESSFULLY !!"<<endl;
obj.close();
}
// FUNCTION TO READ DATA FROM THE FILE
void student :: read_data()
{
fstream obj;
string file_line; // to accept record from file line by line
obj.open("student_data.txt" , ios :: in);
cout<<"=========== FILE DATA =========== "<<endl;
while( obj )
{
getline( obj , file_line );
cout<<endl<<file_line<<endl;
}
obj.close();
}
// -- searching fuction --
void student :: search_data(string search_name)
{
fstream obj;
string file_line;
obj.open("student_data.txt" , ios :: in );
int found = 0;
while( obj )
{
getline( obj , file_line );
size_t pos = file_line.find(search_name);
if( pos != string :: npos)
{
cout<<endl<<": RECORD FOUND :"<<endl;
cout<<endl<<file_line<<endl;
found = 1;
break;
}
}
if(found == 0)
{
cout<<"-- RECORD DOES NOT EXIST --"<<endl;
}
obj.close();
}
void student :: delete_data(string search_name)
{
fstream obj,obj1;
obj.open("student_data.txt" , ios :: in);
obj1.open("temp.txt" , ios :: out);
string file_line;
cout<<endl<<"========== DELETION OF RECORDS ============"<<endl;
while(obj)
{
getline(obj , file_line);
size_t pos = file_line.find(search_name);
if( pos == string :: npos)
{
obj1 << file_line << endl;
}
}
obj.close();
obj1.close();
remove("student_data.txt");
rename("temp.txt","student_data.txt");
cout<<endl<<"........ FILE UPDATED SUCCESSFULLY......."<<endl;
}
int main()
{
student sobj;
sobj.input_data();
sobj.input_data();
sobj.input_data();
sobj.read_data();
sobj.search_data("shreyas");
sobj.delete_data("shreyas");
sobj.read_data();
return 0;
}
Comments
Post a Comment