Write a program that shows the use of read() and write() to handle file I/O involving objects.
#include<iostream.h>
#include<fstream.h>
#include<iomanip.h>
class INVENTORY
{
char name[10];
int code;
float cost;
public:
void readdata(void);
void writedata(void);
};
void INVENTORY :: readdata(void)
{
cout<<"\n Enter name:";cin>>name;
cout<<"\n Enter code:";cin>>code;
cout<<"\n Enter cost:";cin>>cost;
}
void INVENTORY :: writedata(void)
{
cout<<setiosflags(ios::left)
<<setw(10)<<name
<<setiosflags(ios::right)
<<setw(10<<code)
<<setprecision(2)
<<setw(10)<<cost<<endl;
}
int main()
{
INVENTORY item[3];
fstream file;
file.open("Stock.dat",ios::in|ios::out);
cout<<"\n Enter details for three items \n";
for(int i=0;i<3;i++)
{
item[i].readdata();
file.write((char *) & item[i],sizeof(item[i]));
}
file.seekg(0);
cout<<"\nOutput\n";
for(i=0;i<3;i++)
{
file.readdata((char *) &item[i],sizeof(item[i]));
item[i].writedata();
}
file.close();
return 0;
}
#include<fstream.h>
#include<iomanip.h>
class INVENTORY
{
char name[10];
int code;
float cost;
public:
void readdata(void);
void writedata(void);
};
void INVENTORY :: readdata(void)
{
cout<<"\n Enter name:";cin>>name;
cout<<"\n Enter code:";cin>>code;
cout<<"\n Enter cost:";cin>>cost;
}
void INVENTORY :: writedata(void)
{
cout<<setiosflags(ios::left)
<<setw(10)<<name
<<setiosflags(ios::right)
<<setw(10<<code)
<<setprecision(2)
<<setw(10)<<cost<<endl;
}
int main()
{
INVENTORY item[3];
fstream file;
file.open("Stock.dat",ios::in|ios::out);
cout<<"\n Enter details for three items \n";
for(int i=0;i<3;i++)
{
item[i].readdata();
file.write((char *) & item[i],sizeof(item[i]));
}
file.seekg(0);
cout<<"\nOutput\n";
for(i=0;i<3;i++)
{
file.readdata((char *) &item[i],sizeof(item[i]));
item[i].writedata();
}
file.close();
return 0;
}
Really Great Post The Author is very well known about the topic. C++ Programming Complete Notes
ReplyDelete