Oop Unit-V
Oop Unit-V
Contents
• Steams and Files
• Stream classes
• Stream Errors
• File I/O with Streams
• File Pointers
• File I/O with Member functions
• Overloading of extraction and insertion operators
• Memory as stream object
• Command line arguments
• Early V/S Late Binding – in Unit III
C++ I/O
• C++ supports all C set of I/O functions
Program
Output stream
Output insert
device Into
output
stream
• Input stream can come from keyboard or any other storage
device.
istream ostream
iostream
int main()
int main()
{
{ //cout<<"hello";
//cout<<"hello"; char c;
char c;// get a character from c=cin.get();
cin.get(c); // keyboard
while(c!='\n'){
cout<<c;
while(c!='\n'){
cin.get(c);
cout<<c; // display character }
cin.get(c); //on screen & get another getch();
} return 0;
getch(); }
return 0;
}
#include <iostream> #include <iostream>
#include<conio.h>
#include<conio.h>
using namespace std;
using namespace std;
int main()
int main() {
{ cout<<"enter line"<<"\n";
char c;
cout<<"enter line"<<"\n";
c=cin.get();
char c;
c=cin.get(); while(c!='\n'){
cout.put(c);
while(c!='\n'){ cin.get(c);
cout.put(c);
}
cin.get(c);
cout<<"\n";
cout.put(65);
} getch();
getch(); return 0;
return 0;
}
#include <iostream>
#include<conio.h>
getch();
return 0;
}
#include <iostream>
#include<conio.h>
#include<string.h>
cout.write(string1,10);
getch();
return 0;
}
Formatted I/O operations
• Formatting output
• It includes : ios class functions, flags, manipulators, user-
defined output functions
• Manipulators in <iomanip.h>
cout.width(5);
cout<<"123";
cout.width(5);
cout<<"45";// by default right justified
precision
Output truncated to nearest cent
Tailing zeros are truncated
Default precision is 6 digits
cout.precision(3);
cout<<sqrt(2)<<"\n";
cout<<3.14159<<"\n";
cout.precision(5);
cout<<4.2992<<"\n";
Fill and setf
cout.fill('*');
cout.width(10);
cout<<"1234"<<"\n";
cout.setf(ios::left, ios::adjustfield);
cout.width(15);
cout<<"viit"<<"\n";
manipulators
Cout<<setw(5)<<setprecision(2)<<1.24567
Cout<<setw(15)<<setioflags(ios::scientific)<<swrt(3)
File
• in – file is opened for input. out – file is opened for output. binary
– binary file is opened. ate – output position is set to the end of
the file when a file is opened. app - all the outputs are appended
to the existing contents of the file. trunc – erase data from file.
Function Operation
E.g.
file-stream-class stream-object;
stream-object . open (“filename”);
OPENING FILE USING open()
•
Stream-object.open(“filename”, mode)
fstream file;
fstream file;
Class student
{
Public:
Struct stu
{
char name[20];
int roll;
}s;
Void put_data();
Void get_data();
};
void student :: put_data()
{
cout<<"enter name ";
cin>>s. name;
cout<<"enter roll ";
cin>>s. roll;
file. Open ("hit. txt“ , ios :: out | ios :: app);
file. write ((char *)this, sizeof (student));
file. close();
get_data();
}
void student :: get_data()
{
int temp;
cout<<"enter roll no. ";
cin >>temp;
fstream file;
file. open ("hit . txt", ios :: in);
file.seekg(0,ios::beg);
While (file . read ((char *) this, sizeof (student)))
{
If (temp==s. roll)
{
cout<<"student name "<< s . name<<"\n";
cout<<"student roll "<< s . roll;
}
}
getch ();
}
void main()
{
clrscr();
student st;
st .put_data();
}
One more example
fstream file;
//write a message to file file << "This is the first line " << endl
<< "This is the second line" << endl;
file.ignore();
//show the next character without extracting it from file
cout << "Peek " << (char) file.peek() << endl;
int main() {
int integerValue;
cout<<"Before a Bad input operation: "
<<"\ncin.rdstate(): “<<cin.rdstate()
<<"\n cin.eof():"<<cin.eof()
<<"\ncin.fail(): “<<cin.fail()
<<"\ncin.bad(): “<<cin.bad()
<<"\ncin.good(): "<<cin.good()
<<"\n\nExpects an integer, but enter a character: ";
cin>>integerValue;
cout<<endl;
cout<<"After a bad input operation: "
<<"\n cin.rdstate(): "<<cin.rdstate()
<<"\n cin.eof(): "<<cin.eof()
<<"\n cin.fail(): "<<cin.fail()
<<"\n cin.bad(): "<<cin.bad()
<<"\n cin.good(): "<<cin.good()
<<endl<<endl;
cin.clear(ios::goodbit);
cout<<"After cin.clear()"
<<"\ncin.fail(): "<<cin.fail()
<<"\ncin.good(): "<<cin.good()
<<endl<<endl;
return 0; }
Error Handling function in files
RETURN VALUE AND
FUNCTION
MEANING
returns true (non zero) if end
of file is encountered while
eof()
reading; otherwise return
false(zero)
return true when an input or
fail()
output operation has failed
returns true if an invalid
operation is attempted or any
bad()
unrecoverable error has
occurred.
returns true if no error has
good()
occurred.
Command line arguments in C++
• If any input value is passed through command prompt at the time
of running of program is known as command line argument.
Run from command prompt with any no. of values passed at run
time.
Overloading stream insertion (<<) and extraction
(>>) operators
• In C++, stream insertion operator “<<” is used for output
and extraction operator “>>” is used for input.
class Complex{
private:
int real, imag;
public:
Complex(int r = 0, int i =0) {
real = r; imag = i;
}
friend ostream & operator << (ostream &out, const Complex &c);
friend istream & operator >> (istream &in, Complex &c);
};
ostream & operator << (ostream &out, const Complex &c) {
out << c.real;
out << "+i" << c.imag << endl;
return out;
}