Unit-5 Remaining Notes
Unit-5 Remaining Notes
Each file has two associated pointers known as the file pointers. One of them is
called the input pointer and the other is called the output pointer.
ofstream, like ostream, has a pointer known as put pointer that points to
the location where the next element has to be written.
o Append mode
When we open a file in write only mode, the existing contents are
deleted and output pointer is set at the beginning
If we want to open an existing file to add more data, the file is opened
in append mode. This moves the file pointer to the end of the file.
• C++ provides read() and write() functions to read and write the objects
directly.
• This length represents the sum total of lengths of all data members of
the object.
Syntax:
Example
#include<iostream.h>
#include<fstream.h>
class inventory
char name[10];
int code;
float cost;
public:
void readdata(void);
void writedata(void);
};
cout<<“Enter name:”;
cin>> name;
cout<<“Enter code:”;
cin>>code;
cout<<“Enter cost:”;
cin>> cost;
}
void inventory :: writedata(void)
cout<<name;
cout<<code;
cout<<cost;
int main()
inventory item[3];
fstream file;
item[i].readdata();
file.seekg(0);
for(i=0;i<3;i++)
item[i].writedata();
file.close();
return 0;
Example:
#include<iostream.h>
#include<fstream.h>
class inventory
char name[10];
int code;
float cost;
public:
void getdata(void)
void putdata(void)
cout<<name;
cout<<code;
cout<<cost;
};
int main()
inventory item;
fstream inoutfile;
ios::binary);
inoutfile.seekg(0, ios::beg);
item.putdata();
cout<<“Add an item:”;
item.getdata();
inoutfile.seekg(0);
while(inoutfile.read((char * ) & item, sizeof item))
item.putdata();
int n = last/sizeof(item);
cout<<“Number of objects:”<<n;
int object;
cin>> object;
inoutfile.seekp(location);
inoutfile.seekg(0);
item.putdata();
inoutfile.close();
return 0;
Output:
AA 11 100
BB 22 200
CC 33 300
Add an item:
Enter name: DD
Enter code: 44
AA 11 100
BB 22 200
CC 33 300
DD 44 400
Number of objects: 4
Enter name: EE
Enter code: 55
AA 11 100
BB 22 200
CC 33 300
EE 55 500
1. try: Try block consists of the code that may generate exception. Exception are
thrown from inside the try block.
2. throw: Throw keyword is used to throw an exception encountered inside try
block. After the exception is thrown, the control is transferred to catch block.
3. catch: Catch block catches the exception thrown by throw statement from try
block. Then, exception are handled inside catch block.
Syntax
try
{
statements;
... ... ...
throw exception;
}
Syntax
try
{
body of try block
}
Syntax
catch (...)
{
statements;
... ... ...
}
Note: A better way is to use catch(...) as a default statement along with other catch
statement so that it can catch all those exception which are not handled by other catch
statements.
int main()
{
int a,b;
cout << "Enter 2 numbers: ";
cin >> a >> b;
try
{
if (b != 0)
{
float div = (float)a/b;
if (div < 0)
throw 'e';
cout << "a/b = " << div;
}
else
throw e;
}
catch (int e)
{
cout << "Exception: Division by zero";
}
catch (char st)
{
cout << "Exception: Division is less than 1";
}
catch(...)
{
cout << "Exception: Unknown";
}
getch();
return 0;
}
This program demonstrate how exception are handled in C++. This program performs
division operation. Two numbers are entered by user for division operation. If the
dividend is zero, then division by zero will cause exception which is thrown into catch
block. If the answer is less than 0, then exception "Division is less than 1" is thrown.
All other exceptions are handled by the last catch block throwing "Unknown"
exception.
Output
Enter 2 numbers: 8 5
a/b = 1.6
Enter 2 numbers: 9 0
Exception: Division by zero
Enter 2 numbers: -1 10
Exception: Division is less than 1
main() function
We can process the command line arguments using the below main function with arguments
as:
argc
This argument is the count of the arguments. It tells how many command-line arguments
were passed.
argv[]
This argument represents an array of pointers to an array of strings.
Ex:
#include<iostream>
using namespace std;