0% found this document useful (0 votes)
24 views12 pages

Reading and Writing Class Objects

This C++ program demonstrates reading and writing objects to a file. It defines an inventory class with data members to store an item's name, code, and cost. It includes read and write member functions to input and output an inventory object's data. The main function creates an array of inventory objects, reads data into them from the user, and writes the objects to a file. It then reads the data back from the file and displays it, demonstrating reading and writing class objects to a file.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views12 pages

Reading and Writing Class Objects

This C++ program demonstrates reading and writing objects to a file. It defines an inventory class with data members to store an item's name, code, and cost. It includes read and write member functions to input and output an inventory object's data. The main function creates an array of inventory objects, reads data into them from the user, and writes the objects to a file. It then reads the data back from the file and displays it, demonstrating reading and writing class objects to a file.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 12

Reading And Writing Class

Objects
class inventory
{
char name[10];
int code;
float cost;
public:
void readdata();
void writedata();
};
void inventory::readdata()
{
cout<<"Enter name";
cin>>name;
cout<<"Enter code";
cin>>code;
cout<<"Enter cost";
cin>>cost;
}
void inventory::writedata()
{
cout<<name;
cout<<code;
cout<<cost;
}
void main()
{

inventory item[3];
fstream file;
file.open("stock",ios::in|ios::out);
cout<<"Enter details for three items";
for(int i=0;i<3;i++)
{
item[i].readdata();
file.write((char *)&item[i],sizeof(item[i]));
}
file.seekg(0);
cout<<"output";
for(i=0;i<3;i++)
{
file.read((char *)&item[i],sizeof(item[i]));
item[i].writedata();
}
file.close();
getch();
}
Updating A File
1. Displaying the contents of the file.
2. Modifying an existing file.
3. Adding a new item.
4. Deleting an existing item.
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
class inventory
{
char name[10];
int code;
float cost;
public:
void readdata();
void writedata();
};
void inventory::readdata()
{
cout<<"Enter name";
cin>>name;
cout<<"Enter code";
cin>>code;
cout<<"Enter cost";
cin>>cost;
}
void inventory::writedata()
{
cout<<name;
cout<<code;
cout<<cost;
}
void main()
{
clrscr();
inventory item;
fstream file;
file.open("stock",ios::ate|ios::in|ios::out,ios::binary);
file.seekg(0);
cout<<"Current contents of stock";
while(file.read((char*)& item, sizeof(item)))
{
item.writedata();
}
cout<<"Add an item";
item.readdata();
file.write((char *)&item,sizeof(item));
file.seekg(0);
cout<<"Contents of appended file";
while(file.read((char *)&item,sizeof(item)))
{
item.writedata();
}
int last=file.tellg();
int n=last/sizeof(item);
cout<<"Number of objects"<<n;
cout<<"Total bytes inthe file"<<last;
getch();
}
Error Handling During File
Operations
Function Return Value And Meaning

Eof() Returns true(non-zero) if end of file


encountered while reading otherwise
returns false.
Fail() Returns true when an input or output
operation fails.
Bad() Returns true if an invalid operation is
attempted or any unrecoverable error has
occurred.
Good() Returns true if no error has occurred.
This means all the above functions are
false.
Command Line Arguments
In C++, we can supply the arguments to the main() function also.
Example:
#include<fstream.h>
Void main(int argc,char *argv[])
Here argc known as argument counter represents the number of arguments
in the command line and the argument argv known as argument vector is
an array of character type pointers that points to the command line
arguments. The array size is equal to the value of argc .
The values of these arguments are supplied at c:\. Example:
C:\turbo\files>exam data results
Exam- name of file having the program to be executed.
Data, results- file name to be passed as command line arguments.
void main(int argc,char * argv[])
{
int number[9]={11,22,33,44,55,66,77,88,99};
if(argc!=3)
{
cout<<"argc="<<argc;
cout<<"error in argc";
exit(1);
}
ofstream fout1,fout2;
fout1.open(argv[1]);
if(fout1.fail())
{
cout<<"could not open file";
cout<<argv[1];
exit(1);
}
fout2.open(argv[2]);
if(fout2.fail())
{
cout<<"could not open file";
cout<<argv[2];
exit(1);
}
for(int i=0;i<9;i++)
{
if(number[i]%2==0)
{
fout2<<number[i];
}
else
{
fout1<<number[i];
}
}
fout1.close();
fout2.close();
ifstream fin;
char ch;
for(i=0;i<argc;i++)
{
fin.open(argv[i]);
cout<<"contents of "<<argv[i];
do
{
fin.get(ch);
cout<<ch;
}
fin.close();
}
getch();
}

You might also like