SlideShare a Scribd company logo
Chapter: 12


   Stream and Files
              Lecture: 47 and 48
               Date: 12.11.2012
Objectives
   Overview of stream classes

   Showing how to perform file-related activities using streams:
       How to read and write data to files in a variety of ways
       How to handle files or I/O errors
       How files and OOP are related
Streams
   Stream (flow of data)
       A transfer of information in the form of a sequence of bytes

    In C++, a stream is represented by an object of a
    particular class; e.g., cin and cout are objects of
    iostream class.

   I/O Operations:
       Input: A stream that flows from an input device ( i.e.: keyboard,
        disk drive, network connection) to main memory
       Output: A stream that flows from main memory to an output
        device ( i.e.: screen, printer, disk drive, network connection)
keyboard
                  standard
                input stream
                                CPU
                     standard
                      output    MEM
monitor               stream
terminal
console

                                HDD
 What does information
    travel across?
           Streams
keyboard
                  standard
                input stream
                                CPU

                     standard
                      output    MEM
 monitor              stream
 terminal               file
 console               input
                      stream
                      LOAD      HDD
What does information READ
    travel across?                        file
                                files   output
      Streams                           stream
                                        SAVE
                                        WRITE
C++ Stream Input/output
   iostream library has hundreds of I/O capabilities
     iostream: basic input and output
     fstream: file processing

   iostream library contains many I/O related classes:
       istream ( the extraction operator >>, and get(), getline(), read() are
        members of this class)
       ostream (the insertion operator <<, put(), write() are members of
        this class)
   istream and ostream are subclasses of ios base class
   cout is a predefined object of            the iostream_withassign
    class, whereas cin is a                   predefined object of
    istream_withassign class
C++ Stream Input/output
   The classes used for input and output to the video
    display and keyboard are declared in the header file
    iostream, e.g., #include <iostream>

   The classes used specifically for disk file I/O are
    declared in the fstream header file,
    e.g., #include <fstream>

   All of them can be found in the include subdirectory
    of the C++ compiler.
Stream Class Hierarchy
ios Class
   The granddaddy of all the stream classes, and
    contains the majority of the features needed to
    operate C++ streams

   Three most important features of ios class are:
    1)   Formatting flags
    2)   Error-status flags
    3)   File operation mode
ios Formatting Flags

   The formatting flags act as on/off switches that
    specify choices for various aspects of input and
    output format and operation.
Lec 47.48 - stream-files
ios Formatting Flags

   Since they are the members of the ios class, they
    must follow the name ios and the scope resolution
    operator;
    e.g., ios::showpoint
   All the flags must be set using the setf() and usetf()
    ios member functions;
    e.g., cout.setf(ios::showpoint)
          cout.unsetf(ios::showpoint)
#include <iostream>
#include <conio.h>
using namespace std;

int main(void)
{
   float x = 18.0;
   cout<< x << endl;            //displays 18

  cout.setf(ios::showpoint);
  cout<< x << endl;          //displays 18.0000

  cout.setf(ios::scientific);
  cout<< x << endl;             //displays 1.800000e+001

  cout.unsetf(ios::showpoint);
  cout.unsetf(ios::scientific);

   cout<<x<<endl;               //displays 18
getch();
return 0; }
ios Formatting Flags

   Since they are the members of the ios class, they
    must follow the name ios and the scope resolution
    operator;
    e.g., ios::showpoint
   All the flags must be set using the setf() and usetf()
    ios member functions;
    e.g., cout.setf(ios::showpoint)
          cout.unsetf(ios::showpoint)
ios Formatting Flags

   Many formatting flags can be set using manipulators

   Manipulators are formatting instructions inserted
    directly into a stream; e.g., endl, oct, setw() etc.

   Manipulators come in two flavors:
    1)   No-argument ios manipulators
    2)   ios manipulators with arguments
No-argument ios Manipulators
ios Manipulators with Arguments
#include <iostream>
#include <iomanip>
#include <conio.h>
using namespace std;
int main(void)
{
   int var = 11;

  cout << setw(8) << 22 << "n";
  cout << setw(8) << 4444 << "n";
  cout << setw(8) << 666666 << endl;

  cout<< var <<“in hexadecimal is ” << hex <<var;

   cout<< setpercision(5) <<20.99055;
getch();
return 0; }
istream Class

   The istream class is derived from ios class and
    performs input specific activities.
Lec 47.48 - stream-files
istream Class

   cin.get(): inputs a character from stream
    (even white spaces) and returns it

   cin.get( c ): inputs a character from stream
    and stores it in c
istream Class
   cin.get(array, size):
       Accepts 3 arguments: array of characters, the size limit, and a delimiter ( default
        of ‘n’).
       Uses the array as a buffer
       When the delimiter is encountered, it remains in the input stream
       Null character is inserted in the array
       Unless delimiter flushed from stream, it will stay there
   cin.getline(array, size)
       Operates like cin.get(buffer, size) but it discards the delimiter from
        the stream and does not store it in array
       Null character inserted into array
#include <iostream>
#include <conio.h>
using namespace std;

int main()
{

const int SIZE = 10;
char buffer[ SIZE ];

cout << "Enter a sentence:n";
cin.getline( buffer, SIZE );

cout << "nThe sentence entered is:n" << buffer << endl;

getch();
return 0; }
ostream Class

   The ostream class is derived from ios class and
    handles output or insertion activities.
ostream Class
Stream Errors
   Following statements are well-known to us:

 cout<<“Good afternoon”
Or
 cin >> var
What if the user enters “five” instead of “5” for
 integer variable “var” ?
Stream Errors
   Following statements are well-known to us:

 cout<<“Good afternoon”
Or
 cin >> var
What if the user enters “five” instead of “5” for
 integer variable “var” ?

    The compiler will through an error message!
Errors-Status Bits
   The stream error status flags report errors that occur
    in an input or output operation.
Errors-Status Bits
int i;

while(true) // cycle until input OK
 {
  cout << "nEnter an integer: ";
  cin >> i;
  if( cin.good() ) // if no errors
    {
     cin.ignore(10, 'n'); // remove newline
     break; // exit loop
    }
  cin.clear(); // clear the error bits
  cout << "Incorrect input";

  cin.ignore(10, 'n'); // remove newline
 }
cout << "integer is " << i; // error-free integer

More Related Content

PPT
7.0 files and c input
PPT
04 Console input output-
PPT
Abhishek lingineni
PPT
Assembler
PPTX
Introduction to python
PPT
Assembler
PPT
Assembler
PPTX
C++ basics
7.0 files and c input
04 Console input output-
Abhishek lingineni
Assembler
Introduction to python
Assembler
Assembler
C++ basics

What's hot (19)

PDF
C5 c++ development environment
ODP
Perl one-liners
PDF
Embedded C - Lecture 4
PPT
Assembler (2)
ODP
Php opcodes sep2008
ODP
Runtime Symbol Resolution
PDF
N_Asm Assembly numbers (sol)
PPTX
Assembly Language Tutorials for Windows - 03 Assembly Language Programming
ODP
PDF
Something About Dynamic Linking
PPTX
system management -shell programming by gaurav raikar
PPTX
Easiest way to start with Shell scripting
PPT
PDF
Quick tour of PHP from inside
PPTX
Python Programming Essentials - M25 - os and sys modules
PPTX
C5 c++ development environment
Perl one-liners
Embedded C - Lecture 4
Assembler (2)
Php opcodes sep2008
Runtime Symbol Resolution
N_Asm Assembly numbers (sol)
Assembly Language Tutorials for Windows - 03 Assembly Language Programming
Something About Dynamic Linking
system management -shell programming by gaurav raikar
Easiest way to start with Shell scripting
Quick tour of PHP from inside
Python Programming Essentials - M25 - os and sys modules
Ad

Viewers also liked (13)

PPT
Lec 49 - stream-files
PPTX
Lec9 Transistor Bias Circuits
PPT
9781285852744 ppt ch10
PPT
Lec 30.31 - inheritance
PPT
9781285852744 ppt ch11
PPT
File handling in C++
PPTX
Managing console input and output
PPT
File handling
PPTX
PDF
file handling c++
PDF
Object-Oriented Design: Multiple inheritance (C++ and C#)
PPT
Lec 49 - stream-files
Lec9 Transistor Bias Circuits
9781285852744 ppt ch10
Lec 30.31 - inheritance
9781285852744 ppt ch11
File handling in C++
Managing console input and output
File handling
file handling c++
Object-Oriented Design: Multiple inheritance (C++ and C#)
Ad

Similar to Lec 47.48 - stream-files (20)

PPT
098ca session7 c++
PDF
streams and files
PPT
Formatted input and output
PPTX
FILE OPERATIONS.pptx
PDF
Managing I/O in c++
PPTX
basics of file handling
PPTX
Basics of file handling
PPT
iostream_fstream_intro.ppt Upstream iostream
PDF
THE IO LIBRARY in C++
PPTX
Iostream in c++
PPTX
Stream classes in C++
PPTX
programming language in c&c++
PPT
File handling in_c
PPT
file_handling_in_c.pptbbbbbbbbbbbbbbbbbbbbb
PPT
file_handling_in_c.ppt......................................
PPTX
Managing console input
PPTX
File Handling in CPP Programming Language.pptx
PPTX
Managing console of I/o operations & working with files
DOCX
Console i/o for c++
098ca session7 c++
streams and files
Formatted input and output
FILE OPERATIONS.pptx
Managing I/O in c++
basics of file handling
Basics of file handling
iostream_fstream_intro.ppt Upstream iostream
THE IO LIBRARY in C++
Iostream in c++
Stream classes in C++
programming language in c&c++
File handling in_c
file_handling_in_c.pptbbbbbbbbbbbbbbbbbbbbb
file_handling_in_c.ppt......................................
Managing console input
File Handling in CPP Programming Language.pptx
Managing console of I/o operations & working with files
Console i/o for c++

More from Princess Sam (11)

PPT
Lec 50
PPT
Lec 42.43 - virtual.functions
PPT
Lec 40.41 - pointers
PPT
Lec 38.39 - pointers
PPT
Lec 45.46- virtual.functions
PPT
Lec 37 - pointers
PPT
Lec 33 - inheritance
PPT
Lec 28 - operator overloading
PPT
Lec 26.27-operator overloading
PPT
Lec 25 - arrays-strings
PPT
Lec 36 - pointers
Lec 50
Lec 42.43 - virtual.functions
Lec 40.41 - pointers
Lec 38.39 - pointers
Lec 45.46- virtual.functions
Lec 37 - pointers
Lec 33 - inheritance
Lec 28 - operator overloading
Lec 26.27-operator overloading
Lec 25 - arrays-strings
Lec 36 - pointers

Lec 47.48 - stream-files

  • 1. Chapter: 12 Stream and Files Lecture: 47 and 48 Date: 12.11.2012
  • 2. Objectives  Overview of stream classes  Showing how to perform file-related activities using streams:  How to read and write data to files in a variety of ways  How to handle files or I/O errors  How files and OOP are related
  • 3. Streams  Stream (flow of data)  A transfer of information in the form of a sequence of bytes  In C++, a stream is represented by an object of a particular class; e.g., cin and cout are objects of iostream class.  I/O Operations:  Input: A stream that flows from an input device ( i.e.: keyboard, disk drive, network connection) to main memory  Output: A stream that flows from main memory to an output device ( i.e.: screen, printer, disk drive, network connection)
  • 4. keyboard standard input stream CPU standard output MEM monitor stream terminal console HDD What does information travel across? Streams
  • 5. keyboard standard input stream CPU standard output MEM monitor stream terminal file console input stream LOAD HDD What does information READ travel across? file files output Streams stream SAVE WRITE
  • 6. C++ Stream Input/output  iostream library has hundreds of I/O capabilities  iostream: basic input and output  fstream: file processing  iostream library contains many I/O related classes:  istream ( the extraction operator >>, and get(), getline(), read() are members of this class)  ostream (the insertion operator <<, put(), write() are members of this class)  istream and ostream are subclasses of ios base class  cout is a predefined object of the iostream_withassign class, whereas cin is a predefined object of istream_withassign class
  • 7. C++ Stream Input/output  The classes used for input and output to the video display and keyboard are declared in the header file iostream, e.g., #include <iostream>  The classes used specifically for disk file I/O are declared in the fstream header file, e.g., #include <fstream>  All of them can be found in the include subdirectory of the C++ compiler.
  • 9. ios Class  The granddaddy of all the stream classes, and contains the majority of the features needed to operate C++ streams  Three most important features of ios class are: 1) Formatting flags 2) Error-status flags 3) File operation mode
  • 10. ios Formatting Flags  The formatting flags act as on/off switches that specify choices for various aspects of input and output format and operation.
  • 12. ios Formatting Flags  Since they are the members of the ios class, they must follow the name ios and the scope resolution operator; e.g., ios::showpoint  All the flags must be set using the setf() and usetf() ios member functions; e.g., cout.setf(ios::showpoint) cout.unsetf(ios::showpoint)
  • 13. #include <iostream> #include <conio.h> using namespace std; int main(void) { float x = 18.0; cout<< x << endl; //displays 18 cout.setf(ios::showpoint); cout<< x << endl; //displays 18.0000 cout.setf(ios::scientific); cout<< x << endl; //displays 1.800000e+001 cout.unsetf(ios::showpoint); cout.unsetf(ios::scientific); cout<<x<<endl; //displays 18 getch(); return 0; }
  • 14. ios Formatting Flags  Since they are the members of the ios class, they must follow the name ios and the scope resolution operator; e.g., ios::showpoint  All the flags must be set using the setf() and usetf() ios member functions; e.g., cout.setf(ios::showpoint) cout.unsetf(ios::showpoint)
  • 15. ios Formatting Flags  Many formatting flags can be set using manipulators  Manipulators are formatting instructions inserted directly into a stream; e.g., endl, oct, setw() etc.  Manipulators come in two flavors: 1) No-argument ios manipulators 2) ios manipulators with arguments
  • 18. #include <iostream> #include <iomanip> #include <conio.h> using namespace std; int main(void) { int var = 11; cout << setw(8) << 22 << "n"; cout << setw(8) << 4444 << "n"; cout << setw(8) << 666666 << endl; cout<< var <<“in hexadecimal is ” << hex <<var; cout<< setpercision(5) <<20.99055; getch(); return 0; }
  • 19. istream Class  The istream class is derived from ios class and performs input specific activities.
  • 21. istream Class  cin.get(): inputs a character from stream (even white spaces) and returns it  cin.get( c ): inputs a character from stream and stores it in c
  • 22. istream Class  cin.get(array, size):  Accepts 3 arguments: array of characters, the size limit, and a delimiter ( default of ‘n’).  Uses the array as a buffer  When the delimiter is encountered, it remains in the input stream  Null character is inserted in the array  Unless delimiter flushed from stream, it will stay there  cin.getline(array, size)  Operates like cin.get(buffer, size) but it discards the delimiter from the stream and does not store it in array  Null character inserted into array
  • 23. #include <iostream> #include <conio.h> using namespace std; int main() { const int SIZE = 10; char buffer[ SIZE ]; cout << "Enter a sentence:n"; cin.getline( buffer, SIZE ); cout << "nThe sentence entered is:n" << buffer << endl; getch(); return 0; }
  • 24. ostream Class  The ostream class is derived from ios class and handles output or insertion activities.
  • 26. Stream Errors  Following statements are well-known to us: cout<<“Good afternoon” Or cin >> var What if the user enters “five” instead of “5” for integer variable “var” ?
  • 27. Stream Errors  Following statements are well-known to us: cout<<“Good afternoon” Or cin >> var What if the user enters “five” instead of “5” for integer variable “var” ? The compiler will through an error message!
  • 28. Errors-Status Bits  The stream error status flags report errors that occur in an input or output operation.
  • 30. int i; while(true) // cycle until input OK { cout << "nEnter an integer: "; cin >> i; if( cin.good() ) // if no errors { cin.ignore(10, 'n'); // remove newline break; // exit loop } cin.clear(); // clear the error bits cout << "Incorrect input"; cin.ignore(10, 'n'); // remove newline } cout << "integer is " << i; // error-free integer

Editor's Notes

  • #2: Student Book
  • #5: Used tootsie roll pieces as data bytes and a large tootsie roll as a line of data on a cardboard card and drawing on the chalkboard.
  • #6: Used tootsie roll pieces as data bytes and a large tootsie roll as a line of data on a cardboard card and drawing on the chalkboard.