Category Archives: Standard Template Library

C++ Standard Template Library [Map]

Store Different Class Object to File [STL + File I/O Solution]

Problem :

I am having classes in a project like [Student, Customer and many more classes] now I have to manage [Add, Update,

Delete & Display All] all the class data/object to File. Requirement is much efficient manner + minimum overhead.

Much Efficient Solution:

1. Create the 2 Methods for Read All Data & Write All Data Method. [File => STL]

2. Create 4 Methods for Add, Update, Delete , Display. [STL]

Code:


#include
#include
#include

#include

using namespace std;

class Student;

map myData;

template
void writeData(T obj,char *fname);
template
void readData(T obj,char *fname);

void createStudent();
void displayAllStudent();
void displayIDStudent();
void deleteIDStudent();
void updateIDStudent();

//Student Class
class Student
{
int roll;
int marks;

public:

void setData();
void display();
int getID()
{
return roll;
}
};

void Student::setData()
{
cout<>roll;
cout<>marks;
}

void Student::display()
{
cout<<"\n The Roll No:"<<roll;
cout<<"\n The Marks :"<<marks;
}

//Function Definition==================================

//File I/O Add Data
template
void writeData(T obj,char *fname)
{
fstream file;
Student sobj;
map::iterator itr;

file.open(fname,ios::out|ios::binary);

for(itr=myData.begin();itr!=myData.end();itr++)
{
sobj=(*itr).second;
file.write((char *)&sobj,sizeof(sobj));
}
file.close();
}

//File I/O Display Data
template
void readData(T obj,char *fname)
{
int size=0;
int tobj=0;
int i=0;

fstream file;
file.open(fname,ios::in|ios::binary);

if(file.fail())
{
return;
}
else
{
file.seekg(0,ios::end);
size=file.tellg();
tobj=size/sizeof(obj);
file.seekg(0);

for(i=0;i<tobj;i++)
{
file.read((char *)&obj,sizeof(obj));

myData[obj.getID()]=obj;
}
file.close();
}
}

//Write Student Data to Map
void createStudent()
{
Student sobj;
sobj.setData();

myData[sobj.getID()]=sobj;
}

//Read All Student Data from Map
void displayAllStudent()
{
map::iterator itr;

cout<<"\n ============================\n";
for(itr=myData.begin();itr!=myData.end();itr++)
{
(*itr).second.display();
}
cout<<"\n ============================\n";
}

//Reall Student Data from Map base on ID.
void displayIDStudent()
{
int id;
cout<>id;

map::iterator itr;

for(itr=myData.begin();itr!=myData.end();itr++)
{
if(id==(*itr).second.getID())
{
(*itr).second.display();
}
}
}

//Delete Student Data from Map base on ID.
void deleteIDStudent()
{
int id;
Student sobj;
int tid;

cout<>id;

map::iterator itr;

for(itr=myData.begin();itr!=myData.end();itr++)
{
if(id==(*itr).second.getID())
{
sobj=(*itr).second;
tid=sobj.getID();
sobj.display();

myData.erase(tid);
}
}
}

//Update Student Data from Map base on ID.
void updateIDStudent()
{
int id;
Student sobj;
int tid;

cout<>id;

map::iterator itr;

for(itr=myData.begin();itr!=myData.end();itr++)
{
if(id==(*itr).second.getID())
{
(*itr).second.display();

cout<<"\n ========= Enter New Data ==========\n";

sobj.setData();

(*itr).second = sobj;
}
}
}

//Main Method
int main()
{
int choice;
Student sobj;

int end=0;

readData(sobj,"suser.dat");

while(1)
{
cout<<"\n ============================\n";

cout<<"\n 1. Create Student.";
cout<<"\n 2. Display Student ID";
cout<<"\n 3. Display All Student";
cout<<"\n 4. Update Student ID";
cout<<"\n 5. Delete Student ID";

cout<<"\n\n 0. Exit";

cout<<"\n ============================\n";

cout<>choice;

switch(choice)
{
case 1:
createStudent();
break;
case 2:
displayIDStudent();
break;
case 3:
displayAllStudent();
break;
case 4:
updateIDStudent();
break;
case 5:
deleteIDStudent();
break;

case 0:
writeData(sobj,"suser.dat");
end=1;
break;
}

if(end==1)
{
break;
}
}
return 0;
}

Advertisements

Leave a comment

Filed under C++, Standard Template Library