C++_Part2 - Object-oriented Programming With C Lecture...

Info icon This preview shows pages 1–4. Sign up to view the full content.

Object-oriented Programming With C++ Lecture Notes 118 S. Kondakci Page 118 29.04.2004 8. C++ STREAM AND FILE I/O OPERATIONS .............................................................................. 120 8.1 T ERMINAL I/O O PERATIONS .......................................................................................................... 121 8.1.1 Character String Operations ................................................................................................ 121 8.1.2 Buffer String Operations ....................................................................................................... 121 8.2 F ILE I/O O PERATIONS .................................................................................................................... 122 Basic Input Output Operations ...................................................................................................... 123 Character Output With put() ...................................................................................................................... 123 The EOF Character (^Z) ............................................................................................................................ 123 get() and getline() Methods ............................................................................................................ 123 Further Examples: ....................................................................................................................... 125 8.3 T ERMINAL O PERATIONS : S TANDARD I NPUT /O UTPUT .................................................................... 126 8.3.1 Input/Output Buffering ......................................................................................................... 128 8.3.2 Formatted Output ................................................................................................................. 128 Formatting Output With setf() ............................................................................................................. 129 Format Flags .............................................................................................................................................. 129 Functions Used to Modify Format Flags ................................................................................................... 130 Naming Format Flags ................................................................................................................................ 131 9. MANIPULATORS ............................................................................................................................ 132 9.1 U SER D EFINED M ANIPULATORS .................................................................................................... 132 9.2 I NPUT /O UTPUT S TATES ................................................................................................................. 134 10. C++ FILE I/O OPERATIONS ....................................................................................................... 137 10.1 F ILE M ODE O PERATIONS ............................................................................................................. 138 10.2 I/O R EDIRECTION ........................................................................................................................ 138 10.3 C HARACTER - BY -C HARACTER R EAD /W RITE ................................................................................ 139 10.4 C HARACTER - BY -C HARACTER W RITING T O F ILES ....................................................................... 139 10.5 D EFINING I/O F ILE O BJECTS ........................................................................................................ 141 Example: Reading And Writing ................................................................................................................ 142 10.5.1 Reading And Writing With fstream ................................................................................ 143 10.5.2 Reading Lines, Character Strings and Blocks .................................................................... 144 10.6 O UTPUT F UNCTIONS .................................................................................................................... 144 10.6.1 Positions In Streams Objects .............................................................................................. 145 10.7 M EMBER F UNCTIONS OF C HARACTER A RRAY C LASSES .............................................................. 146 10.7.1 Using ostrstream Objects ............................................................................................. 146 10.7.2 Using istrstream Objects ............................................................................................. 147 10.7.3 Using strstream Objects ............................................................................................... 148 10.8 M EMBER F UNCTIONS OF B UFFER C LASSES ................................................................................. 149 10.8.1 Using streambuf Objects ............................................................................................... 149 Input Functions ......................................................................................................................................... 150 Output Functions ....................................................................................................................................... 150 Common Utility Functions ........................................................................................................................ 151 10.9 U SING FILEBUF O BJECTS ............................................................................................................. 151 10.9.1 Using strstreambuf Objects ................................................................................................ 152 Application Example ................................................................................................................................. 154 Exercise: Linked List Version ................................................................................................................... 157 11. INTRODUCTION TO LINKED LISTS ........................................................................................ 158 11.1 E XERCISE : O RGANIZING B OOK R EGISTER I N A B OOKSTORE ........................................................ 162 12. TEMPLATE DRIVEN PROGRAMMING ................................................................................... 172 12.1 C LASS T EMPLATES ...................................................................................................................... 173 12.2 F UNCTION T EMPLATES ................................................................................................................ 176 12.2.1 Explicit Specification of Types ........................................................................................... 178 13. EXCEPTION HANDLING ............................................................................................................ 180
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Object-Oriented Programming With C++ Lecture Notes 119 S. Kondakci Page 119 29.04.2004 13.1 H OW TO R AISE AN E XCEPTION .................................................................................................... 180 13.2 H OW TO C ATCH AN E XCEPTION ................................................................................................... 180 “Out of Memory” ...................................................................................................................................... 182 A Typical Problem ......................................................................................................................... 182
Image of page 2
Object-oriented Programming With C++ Lecture Notes 120 S. Kondakci Page 120 29.04.2004 8. C++ STREAM AND FILE I/O OPERATIONS C++ provides all the terminal and file I/O capabilities of the C language. It also defines a set of library classes to handle file I/O and stream I/O operations. C++ does not directly deal with I/O, it rather uses I/O class objects as intermediate media through which I/O output operations are realized. In C++’s class library the streams plays a central role. A stream is a sequence of characters. Methods from the baseclass ios and its subclasses istream and ostream are used to read from streams, write to streams, control format of streams, and read status about streams operations. The following figure depicts the interface of a user program to the ios class library, which is used to handle I/O operations for the user program. Class istream provides methods for reading and moving around in the input stream.
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern