Lec 47.48 - stream-files

437 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
437
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
15
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • Student Book
  • 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.
  • 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.
  • Lec 47.48 - stream-files

    1. 1. Chapter: 12 Stream and Files Lecture: 47 and 48 Date: 12.11.2012
    2. 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. 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. 4. keyboard standard input stream CPU standard output MEMmonitor streamterminalconsole HDD What does information travel across? Streams
    5. 5. keyboard standard input stream CPU standard output MEM monitor stream terminal file console input stream LOAD HDDWhat does information READ travel across? file files output Streams stream SAVE WRITE
    6. 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. 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.
    8. 8. Stream Class Hierarchy
    9. 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. 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.
    11. 11. 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)
    12. 12. #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 18getch();return 0; }
    13. 13. 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)
    14. 14. 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
    15. 15. No-argument ios Manipulators
    16. 16. ios Manipulators with Arguments
    17. 17. #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; }
    18. 18. istream Class The istream class is derived from ios class and performs input specific activities.
    19. 19. 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
    20. 20. 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
    21. 21. #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; }
    22. 22. ostream Class The ostream class is derived from ios class and handles output or insertion activities.
    23. 23. ostream Class
    24. 24. Stream Errors Following statements are well-known to us: cout<<“Good afternoon”Or cin >> varWhat if the user enters “five” instead of “5” for integer variable “var” ?
    25. 25. Stream Errors Following statements are well-known to us: cout<<“Good afternoon”Or cin >> varWhat if the user enters “five” instead of “5” for integer variable “var” ? The compiler will through an error message!
    26. 26. Errors-Status Bits The stream error status flags report errors that occur in an input or output operation.
    27. 27. Errors-Status Bits
    28. 28. 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

    ×