SlideShare a Scribd company logo
M.SUJITHA,M.SC.,
N.S College, Theni.
C++ STREAM CLASSES
Ios:
It contains basic facilities that are used by all other
input and output classes.
Istream:
It inherites the properties of ios and it declares input
functions such as get(),getline() and read().
Ostream:
It inherites the properties of ios and it declares
output functions such as put() and write().
Iostream:
It inherites the properties of ios stream and ostream.
Streambuf:
It provides an interface to physical devices through
buffers.
The features that supports to format the console
I/O operations are:
◦ Ios class functions and flags
◦ Manipulators
◦ User-defined output functions
IOS CLASS FUNCTIONS AND FLAGS:
It consists of large number of member functions
that is used to format the output in number of ways.
 MANIPULATORS:
These are the special functions that are
included in the I/O statements to format the
parameters of a stream.
 DEFINING FIELD WIDTH:
◦ It is used to define the width of the field.
◦ Can be defined using width();
SETTING PRECISION:
It is used to specify the to be displayed after the
decimal point.
FILLING AND PADDING:
 It is used to print the values using the larger field
widths.
 It can be declared by,
cout.fill();
#include <iostream>
using namespace std;
int main()
{
int item[4] ={ 10,8,12,15};
int cost[4]={75,100,60,99};
cout.width(5);
cout<<”Items”;
cout.width(8);
cout<<”Cost”;
cout.width(15);
cout<<”Total Value”<<”n”;
int sum=0;
for(int i=0;i<4 ;i++)
{
cout.width(5);
cout<<items[i];
cout.width(8);
cout<<cost[i];
int value = items[i] * cost[i];
cout.width(15);
cout<<value<<”n”;
sum= sum + value;
}
cout<<”n Grand total = “;
cout.width(2);
cout<<sum<<”n”;
return 0;
}
OUTPUT:
ITEMS COST TOTAL VALUE
10 75 750
8 100 800
12 60 720
15 99 1485
Grand total =3755
It is used for defining the input and output in
various forms.
Overloaded Operators >> and <<:
• It is used to give the I/O
• The >> is overloaded in istream class
• The << is overloaded in ostream class
Ex:
cin>>item1>>item2;
put() and get() Functions:
◦ It is used for the input and output .
◦ Put(c) is used to give the input
◦ Get(c) is used to get the output
EXAMPLE:
#include <iostream>
using namespace std;
int main()
{
int count=0;
char c;
cout<<”INPUT TEXT n”;
cin.get( c );
while ( c 1=’n’ )
while ( c 1=’n’ )
{
cout.put( c);
count++;
cin.get( c );
}
cout<< “n Number of characters =” <<count <<”n”;
return 0;
}
OUTPUT:
Object oriented programming
Number of characters=27
Getline() and write() Functions:
• The getline() function reads a whole line of the
text and ends with a newline character.
• This function can be invoked by,
cin.getline (line,size);
• The writeline() function reads a whole line of the
text and displays an entire line.
• This function can be invoked by,
cout.write (line,size);
EXAMPLE:
#include <iostream>
using namespace std;
int main()
{
int size=20;
char city[20];
cout<<”enter city name:n “;
cin>>city;
cout<<”city name:”<<city<<”nn”;
cout<<”enter city name again: n”;
cin.getline(city,size);
cout<<”city name now:”<<city<<”nn”;
cout<<”enter another city name: n”;
cin.getline(city,size);
cout <<”New city name:”<<city<<”nn’;
return 0;
}
OUTPUT:
first run
Enter city name:
Delhi
Enter city name again:
City name now:
Enter another city name:
Chennai
New city name:
Chennai
 The header file iomanip provides a set of functions
called manipulators which can be used to manipulate
the output formats.
 They provide the same features as that of the ios
member function and flag.
 Two or more manipulators can be,
Cout<<manip1<<manip1<<manip<<item;
Cout<<manip1<<item1<<manip2<<item2;
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
cout.setf(ios::showpoint);
cout<<setw(5)<<”n”<<setw(15)<<”inverse of
n”<<setw(15)<<”sum of terms”;
double term,sum=0;
for (int n=1;n<=10;n++)
{
term=1.0/float(n);
sum=sum + term;
cout<<setw(5)<<n<<setw(14)<<setprecision(4)<<setiosflags(ios::ss
scientific)<<term<<setw(13)<<resetioflags(ios::scientific<<sum<
<endl;
}
return 0;
}
 The large amount of Data can be handled using some
devices such as floppy disk or hard disk to store those
datas.
 These datas are stored in these devices called FILES.
 The Programs can be designed to perform the read and
write operations on those files.
 Kinds of Data Communication:
◦ Data Transfer between the console unit and the
program.
◦ Data Transfer between the program and a Diskfile.
Managing console i/o operation,working with files
FILEBUF:
It is used to set the file buffers to read and write.
FSTREAMBASE:
It provides operations common to the file streams.
IFSTREAM:
It provides input operations.
OFSTREAM:
It provides output operations.
FSTREAM:
It provides support for both input and output
operations.
 The I/O system contains a set of classes that define
the file handling methods.
 These includes ifstream, ofstream and fstream.
 These classes are derived from fstreambase and from
the corresponding iostream class.
 These classes are designed to manage the Disk files,
which are declared in fstream and we should include
any program in these files.
 A file stream can be declared using the classes
ifstream, ofstream and fstream that are contained in
the Header file fstream.
 A file can be Opened into two ways:
Using the Constructor function of the class
Using the Member function open() of the class
OPENING FILES USING Open():
 The function open() can be used to open multiple
files that use the same stream object.
file-stream-class stream-object;
stream-object.open (“file name”);
#include<iostream.h>
#include<fstream.h>
int main()
{
ofstream fout;
fout.open(“Country names”);
fout<<“Uniited States Of American”;
fout<<“United Kingdomn”;
fout.close();
return 0;
}
OUTPUT:
Country names
United States Of America
United Kingdom
This condition is necessary for preventing data
from the file. This condition is
MORE ABOUT OPEN( ): FILE MODES
• The ifstream and ofstream is used to create
the new files.
• The function open( ) is used to open a new
stream.
object. open(“file name”,mode);
Each file has two associated pointers known
as File pointers.
DEFAULT ACTIONS:
When we open a file in read-only-mode
,these input pointer is automatically set at the
beginning.
FUNCTIONS FOR MANIPULATION OF FILE POINTERS:
 seekg()-Moves a pointer to specified location
 seekp()-Moves put pointer to a specified location
 tellg()-Gives the current position of the get pointer
 tellp()-Gives the current position of the put pointer
It supports a number of member function for
performing the input/output operations on files.
Put() and get() Functions:
◦ It is used to handle single character at a time.
◦ The function put() writes a single character to the
associated stream.
◦ The function get() reads a single character to the
associated stream.
Write() and read() Functions:
It is used to read and write the blocks in the binary
data.
 It is used to maintain the routine task in the data file.
 The updation can be done in:
◦ Displaying the contents of a file
◦ Modifying an existing item
◦ Adding a new file
◦ Deleting an existing file
 The reading and writing files may not be true
always.
◦ A file which we are attempting to open for reading
does not exist.
◦ The file name used for a new file may already exist.
◦ We may use an invalid file name.
◦ There may not be any space in the disk for storing
more data.
◦ We may attempt to perform an operation when the
file is not opened for that purpose.
 This feature facilities the supply of arguments to the
main() function.
 These arguments are supplied at the time of invoking
the program.
◦ C> exam data results
◦ Exam is the name of the file containing the program
to be executed ,data and results are the filenames
passed to the program as command-line arguments.
THANKYOU!!!

More Related Content

PPT
Data members and member functions
PPSX
Support for Object-Oriented Programming (OOP) in C++
PPTX
File in C language
PPTX
Stream classes in C++
PPTX
Dynamic memory allocation in c
PPTX
File handling in Python
PDF
Managing I/O in c++
PDF
C++ Files and Streams
Data members and member functions
Support for Object-Oriented Programming (OOP) in C++
File in C language
Stream classes in C++
Dynamic memory allocation in c
File handling in Python
Managing I/O in c++
C++ Files and Streams

What's hot (20)

PPTX
Polymorphism In c++
PDF
Exception handling
PPTX
Multiplexer and demultiplexer applications.ppsx 3
PPTX
Java exception handling
PPTX
Functions in c language
PPT
Introduction to method overloading &amp; method overriding in java hdm
PPTX
Java(Polymorphism)
PPTX
Exception Handling in C++
PPT
Files in c++ ppt
PPT
C by balaguruswami - e.balagurusamy
PPT
Exception Handling in JAVA
PPTX
Memory mapped I/O and Isolated I/O
PPT
Input and output in C++
PPTX
Selection Statements in C Programming
PPTX
Polymorphism in java
PPTX
Nested loop in C language
PPTX
Multiple inheritance in c++
PPTX
Exception handling c++
PPTX
Inheritance and its types In Java
PPTX
Inter Thread Communicationn.pptx
Polymorphism In c++
Exception handling
Multiplexer and demultiplexer applications.ppsx 3
Java exception handling
Functions in c language
Introduction to method overloading &amp; method overriding in java hdm
Java(Polymorphism)
Exception Handling in C++
Files in c++ ppt
C by balaguruswami - e.balagurusamy
Exception Handling in JAVA
Memory mapped I/O and Isolated I/O
Input and output in C++
Selection Statements in C Programming
Polymorphism in java
Nested loop in C language
Multiple inheritance in c++
Exception handling c++
Inheritance and its types In Java
Inter Thread Communicationn.pptx
Ad

Similar to Managing console i/o operation,working with files (20)

PPTX
Managing console of I/o operations & working with files
PPTX
Introduction to files management systems
PDF
Files in C++.pdf is the notes of cpp for reference
PPTX
13 file handling in C++.pptx oops object oriented programming
PDF
streams and files
PPT
File handling in_c
PPT
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
PPT
file_handling_in_c.ppt......................................
PPT
file_handling_in_c.pptbbbbbbbbbbbbbbbbbbbbb
PPTX
Basics of file handling
PPTX
basics of file handling
PPTX
Object Oriented Programming using C++: Ch12 Streams and Files.pptx
PPTX
Object Oriented Programming Using C++: Ch12 Streams and Files.pptx
PDF
Files and streams
PDF
Filesinc 130512002619-phpapp01
PPSX
Files in c++
PDF
C_and_C++_notes.pdf
PPTX
IOStream.pptx
PPT
cpp-file-handling
PPT
Cpp file-handling
Managing console of I/o operations & working with files
Introduction to files management systems
Files in C++.pdf is the notes of cpp for reference
13 file handling in C++.pptx oops object oriented programming
streams and files
File handling in_c
new pdfrdfzdfzdzzzzzzzzzzzzzzzzzzzzzzzzzzgggggggggggggggggggggggggggggggggggg...
file_handling_in_c.ppt......................................
file_handling_in_c.pptbbbbbbbbbbbbbbbbbbbbb
Basics of file handling
basics of file handling
Object Oriented Programming using C++: Ch12 Streams and Files.pptx
Object Oriented Programming Using C++: Ch12 Streams and Files.pptx
Files and streams
Filesinc 130512002619-phpapp01
Files in c++
C_and_C++_notes.pdf
IOStream.pptx
cpp-file-handling
Cpp file-handling
Ad

More from ramya marichamy (19)

PPTX
NETWORK DEVICE SECURITY NETWORK HARDENING
PPTX
DIGITAL VIDEO DATA SIZING AND OBJECT BASED ANIMATION
PPTX
Image processing
PPTX
Classical encryption techniques
PPTX
Servlets api overview
PPTX
Divide and conquer
PPTX
Region based segmentation
PPTX
Design notation
PPTX
Mining single dimensional boolean association rules from transactional
PPTX
Architecture of data mining system
PPTX
segmentation
PPTX
File Management
PPTX
Arithmetic & Logic Unit
PPTX
SHADOW PAGING and BUFFER MANAGEMENT
PPTX
PPTX
pointer, virtual function and polymorphism
PPTX
Operator overloading
PPTX
microcomputer architecture - Arithmetic instruction
PPTX
High speed lan
NETWORK DEVICE SECURITY NETWORK HARDENING
DIGITAL VIDEO DATA SIZING AND OBJECT BASED ANIMATION
Image processing
Classical encryption techniques
Servlets api overview
Divide and conquer
Region based segmentation
Design notation
Mining single dimensional boolean association rules from transactional
Architecture of data mining system
segmentation
File Management
Arithmetic & Logic Unit
SHADOW PAGING and BUFFER MANAGEMENT
pointer, virtual function and polymorphism
Operator overloading
microcomputer architecture - Arithmetic instruction
High speed lan

Recently uploaded (20)

PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PPTX
Pharma ospi slides which help in ospi learning
PDF
01-Introduction-to-Information-Management.pdf
PDF
Pre independence Education in Inndia.pdf
PPTX
Institutional Correction lecture only . . .
PDF
Classroom Observation Tools for Teachers
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Complications of Minimal Access Surgery at WLH
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Anesthesia in Laparoscopic Surgery in India
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
Pharma ospi slides which help in ospi learning
01-Introduction-to-Information-Management.pdf
Pre independence Education in Inndia.pdf
Institutional Correction lecture only . . .
Classroom Observation Tools for Teachers
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Complications of Minimal Access Surgery at WLH
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Final Presentation General Medicine 03-08-2024.pptx
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
human mycosis Human fungal infections are called human mycosis..pptx
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
O7-L3 Supply Chain Operations - ICLT Program
Renaissance Architecture: A Journey from Faith to Humanism
Anesthesia in Laparoscopic Surgery in India

Managing console i/o operation,working with files

  • 3. Ios: It contains basic facilities that are used by all other input and output classes. Istream: It inherites the properties of ios and it declares input functions such as get(),getline() and read(). Ostream: It inherites the properties of ios and it declares output functions such as put() and write(). Iostream: It inherites the properties of ios stream and ostream. Streambuf: It provides an interface to physical devices through buffers.
  • 4. The features that supports to format the console I/O operations are: ◦ Ios class functions and flags ◦ Manipulators ◦ User-defined output functions IOS CLASS FUNCTIONS AND FLAGS: It consists of large number of member functions that is used to format the output in number of ways.
  • 5.  MANIPULATORS: These are the special functions that are included in the I/O statements to format the parameters of a stream.  DEFINING FIELD WIDTH: ◦ It is used to define the width of the field. ◦ Can be defined using width(); SETTING PRECISION: It is used to specify the to be displayed after the decimal point. FILLING AND PADDING:  It is used to print the values using the larger field widths.  It can be declared by, cout.fill();
  • 6. #include <iostream> using namespace std; int main() { int item[4] ={ 10,8,12,15}; int cost[4]={75,100,60,99}; cout.width(5); cout<<”Items”; cout.width(8); cout<<”Cost”;
  • 7. cout.width(15); cout<<”Total Value”<<”n”; int sum=0; for(int i=0;i<4 ;i++) { cout.width(5); cout<<items[i]; cout.width(8); cout<<cost[i]; int value = items[i] * cost[i]; cout.width(15);
  • 8. cout<<value<<”n”; sum= sum + value; } cout<<”n Grand total = “; cout.width(2); cout<<sum<<”n”; return 0; }
  • 9. OUTPUT: ITEMS COST TOTAL VALUE 10 75 750 8 100 800 12 60 720 15 99 1485 Grand total =3755
  • 10. It is used for defining the input and output in various forms. Overloaded Operators >> and <<: • It is used to give the I/O • The >> is overloaded in istream class • The << is overloaded in ostream class Ex: cin>>item1>>item2;
  • 11. put() and get() Functions: ◦ It is used for the input and output . ◦ Put(c) is used to give the input ◦ Get(c) is used to get the output EXAMPLE: #include <iostream> using namespace std; int main() { int count=0; char c; cout<<”INPUT TEXT n”; cin.get( c ); while ( c 1=’n’ )
  • 12. while ( c 1=’n’ ) { cout.put( c); count++; cin.get( c ); } cout<< “n Number of characters =” <<count <<”n”; return 0; } OUTPUT: Object oriented programming Number of characters=27
  • 13. Getline() and write() Functions: • The getline() function reads a whole line of the text and ends with a newline character. • This function can be invoked by, cin.getline (line,size); • The writeline() function reads a whole line of the text and displays an entire line. • This function can be invoked by, cout.write (line,size);
  • 14. EXAMPLE: #include <iostream> using namespace std; int main() { int size=20; char city[20]; cout<<”enter city name:n “; cin>>city; cout<<”city name:”<<city<<”nn”; cout<<”enter city name again: n”; cin.getline(city,size);
  • 15. cout<<”city name now:”<<city<<”nn”; cout<<”enter another city name: n”; cin.getline(city,size); cout <<”New city name:”<<city<<”nn’; return 0; }
  • 16. OUTPUT: first run Enter city name: Delhi Enter city name again: City name now: Enter another city name: Chennai New city name: Chennai
  • 17.  The header file iomanip provides a set of functions called manipulators which can be used to manipulate the output formats.  They provide the same features as that of the ios member function and flag.  Two or more manipulators can be, Cout<<manip1<<manip1<<manip<<item; Cout<<manip1<<item1<<manip2<<item2;
  • 18. #include<iostream> #include<iomanip> using namespace std; int main() { cout.setf(ios::showpoint); cout<<setw(5)<<”n”<<setw(15)<<”inverse of n”<<setw(15)<<”sum of terms”; double term,sum=0; for (int n=1;n<=10;n++)
  • 20.  The large amount of Data can be handled using some devices such as floppy disk or hard disk to store those datas.  These datas are stored in these devices called FILES.  The Programs can be designed to perform the read and write operations on those files.  Kinds of Data Communication: ◦ Data Transfer between the console unit and the program. ◦ Data Transfer between the program and a Diskfile.
  • 22. FILEBUF: It is used to set the file buffers to read and write. FSTREAMBASE: It provides operations common to the file streams. IFSTREAM: It provides input operations. OFSTREAM: It provides output operations. FSTREAM: It provides support for both input and output operations.
  • 23.  The I/O system contains a set of classes that define the file handling methods.  These includes ifstream, ofstream and fstream.  These classes are derived from fstreambase and from the corresponding iostream class.  These classes are designed to manage the Disk files, which are declared in fstream and we should include any program in these files.
  • 24.  A file stream can be declared using the classes ifstream, ofstream and fstream that are contained in the Header file fstream.  A file can be Opened into two ways: Using the Constructor function of the class Using the Member function open() of the class OPENING FILES USING Open():  The function open() can be used to open multiple files that use the same stream object. file-stream-class stream-object; stream-object.open (“file name”);
  • 25. #include<iostream.h> #include<fstream.h> int main() { ofstream fout; fout.open(“Country names”); fout<<“Uniited States Of American”; fout<<“United Kingdomn”; fout.close(); return 0;
  • 26. } OUTPUT: Country names United States Of America United Kingdom
  • 27. This condition is necessary for preventing data from the file. This condition is MORE ABOUT OPEN( ): FILE MODES • The ifstream and ofstream is used to create the new files. • The function open( ) is used to open a new stream. object. open(“file name”,mode);
  • 28. Each file has two associated pointers known as File pointers. DEFAULT ACTIONS: When we open a file in read-only-mode ,these input pointer is automatically set at the beginning. FUNCTIONS FOR MANIPULATION OF FILE POINTERS:  seekg()-Moves a pointer to specified location  seekp()-Moves put pointer to a specified location  tellg()-Gives the current position of the get pointer  tellp()-Gives the current position of the put pointer
  • 29. It supports a number of member function for performing the input/output operations on files. Put() and get() Functions: ◦ It is used to handle single character at a time. ◦ The function put() writes a single character to the associated stream. ◦ The function get() reads a single character to the associated stream. Write() and read() Functions: It is used to read and write the blocks in the binary data.
  • 30.  It is used to maintain the routine task in the data file.  The updation can be done in: ◦ Displaying the contents of a file ◦ Modifying an existing item ◦ Adding a new file ◦ Deleting an existing file
  • 31.  The reading and writing files may not be true always. ◦ A file which we are attempting to open for reading does not exist. ◦ The file name used for a new file may already exist. ◦ We may use an invalid file name. ◦ There may not be any space in the disk for storing more data. ◦ We may attempt to perform an operation when the file is not opened for that purpose.
  • 32.  This feature facilities the supply of arguments to the main() function.  These arguments are supplied at the time of invoking the program. ◦ C> exam data results ◦ Exam is the name of the file containing the program to be executed ,data and results are the filenames passed to the program as command-line arguments.