Stream classes in C++

22
Stream Classes IN C++ BY Roll No:- 11 13 14 15

Transcript of Stream classes in C++

Stream Classes

IN

C++ BY

Roll No:-11131415

What Is C++?

▪ C++, as the name suggests, is a superset of C.▪ C++ can run most of C code while C cannot run C++ code.

▪ C follows the Procedural Programming Language(POP)▪ while C++ is a language(procedural as well as object oriented)

▪ In C the data is not secured while in C++ data is secured(hidden) .

▪ Encapsulation OF Data Is Done In C++

▪ In C Importance is given on doing Things(Algorithm) While In C++ importance are given on Data (Object).

▪ C uses the top-down approach while C++ uses the bottom-up approach.

What Is Stream? What is its needs in c++?

A stream is nothing but a flow of data. In the object-oriented programming, the streams are controlled using the classes. The operations with the files mainly consist of two types. They are read and write. C++ provides various classes, to perform these operations. The ios class is the base class. All other classes are derived from the ios class. These classes contain several member functions that perform input and output operations.

Different streams are used to represent different kinds of data flow.

Each stream is associated with a particular class, which contains member functions and definitions for dealing with that particular kind of data flow.

The stream that supplies data to the program in known as an input stream. It reads the data from the file and hands it over to the program. The stream that receives data from the program is known as an output stream. It writes the received data to the file

The following Figure illustrates This.

The Istream and ostream classes control input and output functions,respectively. The ios is the base class of these two classes. The memberfunctions of these classes handle formatted and unformatted operations.

ios

IstreamUsed for INPUT

function

OstreamUsed for Output

Function

File Class Hierarchy

ios

istream ostream

iostream

ifstream fstream ofstream

iostream.h

fstream.h

InputOutput

File I/O

Multiple inheritanceBase Class

steambuf

filebuf

Header file Brief description

<iostream>Provide basic information required for all stream I/O operation such as cin, cout, cerr and clog correspond to standard input stream, standard output stream, and standard unbuffered and buffered error streams respectively.

<iomanip> Contains information useful for performing formatted I/O with parameterized stream manipulation.

<fstream> Contains information for user controlled file processing operations.

<strstream>Contains information for performing in-memory formatting or in-core formatting.  This resembles file processing, but the I/O operation is performed to and from character arrays rather than files.

<stdiostrem>Contains information for program that mixes the C and C++ styles of I/O.

 

iostream Library

Data Type Description

ofstreamThis data type represents the output file stream and is used to create files and to write information to files.

ifstreamThis data type represents the input file stream and is used to read information from files.

fstream

This data type represents the file stream generally, and has the capabilities of both ofstream and ifstream which means it can create files, write information to files, and read information from files.

To perform file processing in c++, header files <iostream> and <fstream> must be included in your c++ source file.

Data Types In Ostream classThis data types requires another standard C++ library called fstream, which defines three new data types:

Mode Flag Description

ios::app Append mode. All output to that file to be appended to the end.

ios::ateOpen a file for output and move the read/write control to the end of the file.

ios::in Open a file for reading.ios::out Open a file for writing.

ios::truncIf the file already exists, its contents will be truncated before opening the file.

Mode Flag Of iostream

Opening a File:

A file must be opened before you can read from it or write to it. Either the ofstream or fstream object may be used to open a file for writing

And ifstream object is used to open a file for reading purpose only.

void open(const char *filename, ios::openmode mode);

Closing a File

When a C++ program terminates it automatically closes flushes all the streams, release all the allocated memory and close all the opened files. But it is always a good practice that a programmer should close all the opened files before program termination.

Following is the standard syntax for close() function, which is a member of fstream, ifstream, and ofstream objects.

void close();

Operators Brief descriptioncin Object of istream class, connected to the standard input device, normally the keyboard.

cout Object of ostream class, connected to standard output device, normally the display/screen.

cerr Object of the ostream class connected to standard error device.  This is unbuffered output, so each insertion to cerr causes its output to appear immediately.

clog Same as cerr but outputs to clog are buffered.

Writing to a File:The left shift operator (<<) is overloaded to designate stream output and is called stream insertion operator.

write information to a file from your program using thestream insertion operator (<<) just as you use that operator to output information to thescreen. The only difference is that you use an of stream or fstream object instead of the cout object.

Reading from a File:The right shift operator (>>) is overloaded to designate stream input and is called stream extraction operator. You read information from a file into your program using the stream extraction operator (>>)just as you use that operator to input information from the keyboard. The only difference is that you use an Ifstream or Fstream object instead of the Cin object.

Read & Write Example:-

// string output using <<#include <iostream>void main(){cout<<"Welcome to C++ I/O module!!!"<<endl;cout<<"Welcome to ";cout<<"C++ module 18"<<endl;  // endl is end line stream manipulator}  

Stream output program example:-

Output:

Stream input program example:-

#include <iostream.h> 

void main()

{

int p, q, r;

cout << "Enter 3 integers separated by space: \n";

cin>>p>>q>>r; // the >> operator skips whitespace characters

cout<<"Sum of the "<<p<<","<<q<<" and "<<r<<" is = "<<(p+q+r)<<endl;

}

 

 

Output:

From File Read & Write Example using fstream class #include <fstream>

#include <iostream>

using namespace std;

int main ()

{

char data[100];

ofstream outfile;

outfile.open("afile.dat"); // open a file in write mode.

cout << "Writing to the file" << endl;

cout << "Enter your name: ";

cin.getline(data, 100);

outfile << data << endl;

cout << "Enter your age: "; // write inputted data into the file.

cin >> data;

cin.ignore();

outfile << data << endl; // again write inputted data into the file.

outfile.close(); // close the opened file.

ifstream infile; // open a file in read mode.

infile.open("afile.dat");

cout << "Reading from the file" << endl;

infile >> data;

cout << data << endl; // write the data at the screen.

infile >> data; // again read the data from the file and display it.

cout << data << endl;

infile.close(); // close the opened file.

return 0;

}

Writing to the fileEnter your name: XYZ..Enter your age: 9

Reading from the fileXYZ..9

Output

filebuf accomplishes input and output operations with files. The streambuf class does not organize streams for input or output operations.

The derived classes of streambuf perform these operations. It also arranges a spacefor keeping input data and for sending output.

The filebuf class is a derived class of streambuf that is specialized for buffered disk file I/O. The buffering is managed entirely within the iostream Class Library. 

filebuf member functions call the run-time low-level I/O routines (the functions declared in <iostream.h>

Filebuf class in iostream

The I/O functions of the classesistream and ostream invoke the filebuf functions to perform the insertion orextraction on the streams

The fstreambase acts as a base class for fstream, ifstream,and ofstream.It holds constant openprototype used in function open() and close() as a member.

The fstream: It allows both simultaneous input and output operations on a filebuf.The member function of the base classes istream and ostream starts the input and output.

The ofstream: This class is derived from fstreambase and ostream classes. It can access the member functions such as put() , write() ,display() and It allows output operations and provides the member function with thedefault output mode.

The ifstream: This class is derived from fstreambase and istream by multiple inheritance. It can access the member functions such as get(), getline() , and read()It allows input operations function with the default input mode.

Program of class student having data member having rollno,name,address.Accept And Display data for one object

#include<iostream.h> //stream header file and its library is used#include<conio.h>class student //class as student is declared.{Int roll; variables initializedChar name[20];Public: //Access specified as public.Void accept() // “istream “ is used to accept data from user.{Cout<<“\n Enter the Name Of student:-”; // cout operator is used to display on screenCin>>name; // cin operator is used to stored vale on disk,Cout<<“\n Enter the roll no of student:-”;Cin >>roll; }Void display(){Cout<<“\n Name of Student :-”<< name; // cout operator is used to display on screenCout<<“\n Roll No Of Student:-”<<roll; // cout operator is used to display on screen}

“<<“ & “>>” operator are used for insertion and extraction of

data from file .

BottomUp

Approach

Void display(){Cout<<“\n Name of Student :-”<< name; // cout operator is used to display on screenCout<<“\n Roll No Of Student:-”<<roll; // cout operator is used to display on screen} };Void main(){Student s1S1.accept();S1.display();Getch();}

BottomUp

Approach

Output

Enter The Name Of Student:- PPTEnter The Roll No:- 001

Name Of The Student Is:- PPTRoll No :-001

Thank You…..