SlideShare a Scribd company logo
1 of 22
Stream Classes
IN
C++
BY
Roll No:-
11
13
14
15
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 doingThings(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 illustratesThis.
The Istream and ostream classes
control input and output functions,
respectively.
The ios is the base class of these two
classes.
The member
functions of these classes handle
formatted and unformatted operations.
ios
Istream
Used for INPUT
function
Ostream
Used for Output
Function
File Class Hierarchy
ios
istream ostream
iostream
ifstream fstream ofstream
iostream.h
fstream.h
Input
Output
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
DataType Description
ofstream
This data type represents the output file
stream and is used to create files and to write
information to files.
ifstream
This 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.
DataTypes In Ostream class
This 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::ate
Open 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::trunc
If 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 description
cin 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 the
stream insertion operator (<<) just as you use that operator to output information to the
screen.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 file
Enter your name: XYZ..
Enter your age: 9
Reading from the file
XYZ..
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.
 filebufmember 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 classes
istream
and
ostream
invoke the filebuf functions to perform the insertion or
extraction 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 initialized
Char 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 screen
Cin>>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 screen
Cout<<“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 .
Bottom
Up
Approach
Void display()
{
Cout<<“n Name of Student :-”<< name; // cout operator is used to display on screen
Cout<<“n Roll No Of Student:-”<<roll; // cout operator is used to display on screen
}
};
Void main()
{
Student s1
S1.accept();
S1.display();
Getch();
}
Bottom
Up
Approach
Output
EnterThe Name Of Student:- PPT
EnterThe Roll No:- 001
Name Of The Student Is:- PPT
Roll No :-001
Thank You…..

More Related Content

What's hot

Chapter 7 - Input Output Statements in C++
Chapter 7 - Input Output Statements in C++Chapter 7 - Input Output Statements in C++
Chapter 7 - Input Output Statements in C++
Deepak Singh
 
Inline function
Inline functionInline function
Inline function
Tech_MX
 

What's hot (20)

Functions in C
Functions in CFunctions in C
Functions in C
 
File handling in c
File handling in cFile handling in c
File handling in c
 
Data types in c++
Data types in c++Data types in c++
Data types in c++
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
 
C++ Files and Streams
C++ Files and Streams C++ Files and Streams
C++ Files and Streams
 
FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
 
Function overloading
Function overloadingFunction overloading
Function overloading
 
Chapter 7 - Input Output Statements in C++
Chapter 7 - Input Output Statements in C++Chapter 7 - Input Output Statements in C++
Chapter 7 - Input Output Statements in C++
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Inline function
Inline functionInline function
Inline function
 
07. Virtual Functions
07. Virtual Functions07. Virtual Functions
07. Virtual Functions
 
Function overloading(c++)
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
 
File Handling In C++
File Handling In C++File Handling In C++
File Handling In C++
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
File in c
File in cFile in c
File in c
 
Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C Language
 
Input and output in C++
Input and output in C++Input and output in C++
Input and output in C++
 
Basic concept of OOP's
Basic concept of OOP'sBasic concept of OOP's
Basic concept of OOP's
 

Viewers also liked

file handling c++
file handling c++file handling c++
file handling c++
Guddu Spy
 
Files in c++ ppt
Files in c++ pptFiles in c++ ppt
Files in c++ ppt
Kumar
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
Alok Kumar
 

Viewers also liked (20)

cpp input & output system basics
cpp input & output system basicscpp input & output system basics
cpp input & output system basics
 
file handling c++
file handling c++file handling c++
file handling c++
 
Files in c++ ppt
Files in c++ pptFiles in c++ ppt
Files in c++ ppt
 
File Handling in C++
File Handling in C++File Handling in C++
File Handling in C++
 
C++ io manipulation
C++ io manipulationC++ io manipulation
C++ io manipulation
 
2CPP17 - File IO
2CPP17 - File IO2CPP17 - File IO
2CPP17 - File IO
 
Managing console input and output
Managing console input and outputManaging console input and output
Managing console input and output
 
C++ classes
C++ classesC++ classes
C++ classes
 
C++ Pointers
C++ PointersC++ Pointers
C++ Pointers
 
Unit 6 pointers
Unit 6   pointersUnit 6   pointers
Unit 6 pointers
 
Oop c++class(final).ppt
Oop c++class(final).pptOop c++class(final).ppt
Oop c++class(final).ppt
 
Chap 12(files)
Chap 12(files)Chap 12(files)
Chap 12(files)
 
Union In language C
Union In language CUnion In language C
Union In language C
 
working file handling in cpp overview
working file handling in cpp overviewworking file handling in cpp overview
working file handling in cpp overview
 
File Handling In C++(OOPs))
File Handling In C++(OOPs))File Handling In C++(OOPs))
File Handling In C++(OOPs))
 
Linked list
Linked listLinked list
Linked list
 
Input output streams
Input output streamsInput output streams
Input output streams
 
Variables and data types in C++
Variables and data types in C++Variables and data types in C++
Variables and data types in C++
 
A presentation on types of network
A presentation on types of networkA presentation on types of network
A presentation on types of network
 
Overloading of io stream operators
Overloading of io stream operatorsOverloading of io stream operators
Overloading of io stream operators
 

Similar to Stream classes in C++

basics of file handling
basics of file handlingbasics of file handling
basics of file handling
pinkpreet_kaur
 
Basics of file handling
Basics of file handlingBasics of file handling
Basics of file handling
pinkpreet_kaur
 
Files in C++.pdf is the notes of cpp for reference
Files in C++.pdf is the notes of cpp for referenceFiles in C++.pdf is the notes of cpp for reference
Files in C++.pdf is the notes of cpp for reference
anuvayalil5525
 
Data file handling
Data file handlingData file handling
Data file handling
TAlha MAlik
 
Input File dalam C++
Input File dalam C++Input File dalam C++
Input File dalam C++
Teguh Nugraha
 
Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01
Rex Joe
 
Chpater29 operation-on-file
Chpater29 operation-on-fileChpater29 operation-on-file
Chpater29 operation-on-file
Deepak Singh
 

Similar to Stream classes in C++ (20)

basics of file handling
basics of file handlingbasics of file handling
basics of file handling
 
Basics of file handling
Basics of file handlingBasics of file handling
Basics of file handling
 
File handling in_c
File handling in_cFile handling in_c
File handling in_c
 
File management in C++
File management in C++File management in C++
File management in C++
 
Managing console i/o operation,working with files
Managing console i/o operation,working with filesManaging console i/o operation,working with files
Managing console i/o operation,working with files
 
Managing,working with files
Managing,working with filesManaging,working with files
Managing,working with files
 
Files in C++.pdf is the notes of cpp for reference
Files in C++.pdf is the notes of cpp for referenceFiles in C++.pdf is the notes of cpp for reference
Files in C++.pdf is the notes of cpp for reference
 
File in cpp 2016
File in cpp 2016 File in cpp 2016
File in cpp 2016
 
Streams and Files
Streams and FilesStreams and Files
Streams and Files
 
streams and files
 streams and files streams and files
streams and files
 
Data file handling
Data file handlingData file handling
Data file handling
 
Input File dalam C++
Input File dalam C++Input File dalam C++
Input File dalam C++
 
Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01Filesinc 130512002619-phpapp01
Filesinc 130512002619-phpapp01
 
File handling in cpp
File handling in cppFile handling in cpp
File handling in cpp
 
Chapter4.pptx
Chapter4.pptxChapter4.pptx
Chapter4.pptx
 
working with files
working with filesworking with files
working with files
 
Input output files in java
Input output files in javaInput output files in java
Input output files in java
 
file management functions
file management functionsfile management functions
file management functions
 
Chpater29 operation-on-file
Chpater29 operation-on-fileChpater29 operation-on-file
Chpater29 operation-on-file
 
C_and_C++_notes.pdf
C_and_C++_notes.pdfC_and_C++_notes.pdf
C_and_C++_notes.pdf
 

Recently uploaded

%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
VictoriaMetrics
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
masabamasaba
 

Recently uploaded (20)

%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
WSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - KeynoteWSO2Con204 - Hard Rock Presentation - Keynote
WSO2Con204 - Hard Rock Presentation - Keynote
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 

Stream classes in C++

  • 2. 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 doingThings(Algorithm) While In C++ importance are given on Data (Object). ▪ C uses the top-down approach while C++ uses the bottom-up approach.
  • 3. 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.
  • 4. 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 illustratesThis.
  • 5. The Istream and ostream classes control input and output functions, respectively. The ios is the base class of these two classes. The member functions of these classes handle formatted and unformatted operations. ios Istream Used for INPUT function Ostream Used for Output Function
  • 6. File Class Hierarchy ios istream ostream iostream ifstream fstream ofstream iostream.h fstream.h Input Output File I/O Multiple inheritanceBase Class steambuf filebuf
  • 7. 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
  • 8. DataType Description ofstream This data type represents the output file stream and is used to create files and to write information to files. ifstream This 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. DataTypes In Ostream class This data types requires another standard C++ library called fstream, which defines three new data types:
  • 9. Mode Flag Description ios::app Append mode. All output to that file to be appended to the end. ios::ate Open 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::trunc If 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);
  • 10. 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();
  • 11. Operators Brief description cin 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 the stream insertion operator (<<) just as you use that operator to output information to the screen.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.
  • 12. 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:
  • 13.  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:
  • 14. 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.
  • 15. 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; }
  • 16. Writing to the file Enter your name: XYZ.. Enter your age: 9 Reading from the file XYZ.. 9 Output
  • 17.  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.  filebufmember functions call the run-time low-level I/O routines (the functions declared in <iostream.h> Filebuf class in iostream
  • 18. The I/O functions of the classes istream and ostream invoke the filebuf functions to perform the insertion or extraction 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.
  • 19. 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.
  • 20. 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 initialized Char 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 screen Cin>>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 screen Cout<<“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 . Bottom Up Approach
  • 21. Void display() { Cout<<“n Name of Student :-”<< name; // cout operator is used to display on screen Cout<<“n Roll No Of Student:-”<<roll; // cout operator is used to display on screen } }; Void main() { Student s1 S1.accept(); S1.display(); Getch(); } Bottom Up Approach Output EnterThe Name Of Student:- PPT EnterThe Roll No:- 001 Name Of The Student Is:- PPT Roll No :-001