Sei sulla pagina 1di 5

//Reading and writing single object of class using file

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
class inventory
{
char name[30];
int code;
float cost;
public:
void getdata(void)
{
cout<<"Enter Name:";
cin>>name;
cout<<"\nEnter Code:";
cin>>code;
cout<<"\nEnter Cost:";
cin>>cost;
}
void putdata(void)
{
cout<<"Name="<<name;
cout<<"\nCode="<<code;
cout<<"\nCost="<<cost;
}
};
int main()
{
inventory i1;//class object
fstream file1;//file object
file1.open("Invent_Item",ios::ate|ios::in|ios::out|
ios::binary);// Create file
cout<<\nEnter the details about items;
i1.getdata();
file1.write((char *) & i1,sizeof(i1));//Write file
file1.seekg(0,ios::beg);//Set pointer at the file begining
cout<<Display the data from file;
file1.read((char *) & i1,sizeof(i1));//Read file
i1.putdata();
file1.close();
getch();
return 0;
}
//Reading and writing multiple object of class using file

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
class inventory
{
char name[30];
int code;
float cost;
public:
void getdata(void)
{
cout<<"Enter Name:";
cin>>name;
cout<<"\nEnter Code:";
cin>>code;
cout<<"\nEnter Cost:";
cin>>cost;
}
void putdata(void)
{
cout<<"Name="<<name;
cout<<"\nCode="<<code;
cout<<"\nCost="<<cost;
}
};
int main()
{
inventory i1[3];
fstream file1;
file1.open("Invent_Item",ios::ate|ios::in|ios::out|
ios::binary);
cout<<\nEnter the details about items;
for(int i=0;i<3;i++)
{
i1[i].getdata();
file1.write((char *) & i1[i],sizeof(i1[i]));//Write in
file
}
file1.seekg(0,ios::beg);//Set pointer at the file begining
cout<<Display the data from file;
for(i=0;i<3;i++)
{
file1.read((char *) & i1[i],sizeof(i1[i]));//read file
i1[i].putdata();
}
file1.close();
getch();
return 0;
}

//Display the data of existing file and add new data in file using class object

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
class inventory
{
char name[30];
int code;
float cost;
public:
void getdata(void)
{
cout<<"Enter Name:";
cin>>name;
cout<<"\nEnter Code:";
cin>>code;
cout<<"\nEnter Cost:";
cin>>cost;
}
void putdata(void)
{
cout<<"\nName="<<name;
cout<<"\nCode="<<code;
cout<<"\nCost="<<cost;
}
};
int main()
{
inventory i1;
fstream file1;
clrscr();
file1.open("Invent_Item",ios::ate|ios::in|ios::out|
ios::binary);
file1.seekg(0,ios::beg);
cout<<"Display the existing content";
while(file1.read((char *) & i1,sizeof(i1)))
{
i1.putdata();
}
file1.clear();
cout<<"\n Add new data:";
i1.getdata();
file1.write((char *) & i1,sizeof(i1));
cout<<"Display all records";
file1.seekg(0,ios::beg);
while(file1.read((char *) & i1,sizeof(i1)))
{
i1.putdata();
}
file1.close();
getch();
return 0;
}

//Updateing a file-

//1:Display Existing content 2:Modifying existing Data 3:Add new


data 4:Delete existing data

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
class inventory
{
char name[30];
int code;
float cost;
public:
void getdata(void)
{
cout<<"Enter Name:";
cin>>name;
cout<<"\nEnter Code:";
cin>>code;
cout<<"\nEnter Cost:";
cin>>cost;
}
void putdata(void)
{
cout<<"\nName="<<name;
cout<<"\nCode="<<code;
cout<<"\nCost="<<cost;
}
};
int main()
{
inventory i1;
fstream file1;
clrscr();
file1.open("Invent_Item",ios::ate|ios::in|ios::out|
ios::binary);
file1.seekg(0,ios::beg);
cout<<"Display the existing content";
while(file1.read((char *) & i1,sizeof(i1)))
{
i1.putdata();
}
file1.clear();
int obj_size;
obj_size=sizeof(i1);//Size of single object
int last;
last=file1.tellg();//Currenbt position og get pointer
int n;
n=last/obj_size;//Number of object in file
cout<<"\No of object="<<n;
cout<<"\nTotal number of bytes in file="<<last;
cout<<"\n1Single object size="<<obj_size;

int obj_no;
cout<<"\nEnter object no to be updated";
cin>>obj_no;

int location;
location=(obj_no-1) * obj_size;//Location of object
cout<<"\nLocation="<<location;
file1.seekp(location);//go to particular location

cout<<"\n Add new data:";


i1.getdata();
file1.write((char *) & i1,sizeof(i1));
cout<<"Display all records";
file1.seekg(0,ios::beg);//go to start
while(file1.read((char *) & i1,sizeof(i1)))
{
i1.putdata();
}
file1.close();
getch();
return 0;
}

Potrebbero piacerti anche