Skip to main content Accessibility help
×
Hostname: page-component-77c89778f8-swr86 Total loading time: 0 Render date: 2024-07-22T14:21:10.225Z Has data issue: false hasContentIssue false

Chapter 10 - Input and output streams

Published online by Cambridge University Press:  05 June 2012

David Yevick
Affiliation:
University of Waterloo, Ontario
Get access

Summary

C++ implements a device-independent approach to reading and writing data such that nearly the same interface (the public class members) is employed to send data to or receive data from different devices such as disk files, memory buffers and the keyboard and terminal. This is achieved by collecting functions and variables common to all input and output operations into an ios base class. Operations involving the standard input device (keyboard) cin are appended in a derived istream class, while functionality that enables data to be read from files and memory buffers is incorporated in the ifstream and istrstream subclasses of istream, respectively. Output operations are implemented in the same manner by the corresponding output classes, ostream, ofstream and ostrstream. The iostream class multiply inherits the internal functions and variables of both the istream and ostream classes. Finally, the fstream and strstream classes further specialize the iostream class by appending the properties required to both read and write to files and memory buffers (alternatively e.g. fstream can multiply inherit from ofstream and ifstream). Following the structure of the inheritance diagram, we first discuss the iostream class. Subsequently we discuss the interfaces to files and memory buffers associated with fstream and strstream.

The iostream class and stream manipulators

In C++, variables read into or written from a program initially reside in abstract objects termed streams, examples of which are cin and cout. These streams may be thought of as smart memory buffers enhanced through the inclusion of public, user-accessible functions that can act on the data stored in the stream buffer before its contents are processed by the program or sent to a device. The components that interface the memory buffer with the actual system devices are implemented as private functions and variables of the stream classes and are invisible to the user.

Type
Chapter
Information
A Short Course in Computational Science and Engineering
C++, Java and Octave Numerical Programming with Free Software Tools
, pp. 93 - 100
Publisher: Cambridge University Press
Print publication year: 2012

Access options

Get access to the full version of this content by using one of the access options below. (Log in options will check for institutional or personal access. Content may require purchase if you do not have access.)

Save book to Kindle

To save this book to your Kindle, first ensure coreplatform@cambridge.org is added to your Approved Personal Document E-mail List under your Personal Document Settings on the Manage Your Content and Devices page of your Amazon account. Then enter the ‘name’ part of your Kindle email address below. Find out more about saving to your Kindle.

Note you can select to save to either the @free.kindle.com or @kindle.com variations. ‘@free.kindle.com’ emails are free but can only be saved to your device when it is connected to wi-fi. ‘@kindle.com’ emails can be delivered even when you are not connected to wi-fi, but note that service fees apply.

Find out more about the Kindle Personal Document Service.

  • Input and output streams
  • David Yevick, University of Waterloo, Ontario
  • Book: A Short Course in Computational Science and Engineering
  • Online publication: 05 June 2012
  • Chapter DOI: https://doi.org/10.1017/CBO9781139022262.010
Available formats
×

Save book to Dropbox

To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Dropbox.

  • Input and output streams
  • David Yevick, University of Waterloo, Ontario
  • Book: A Short Course in Computational Science and Engineering
  • Online publication: 05 June 2012
  • Chapter DOI: https://doi.org/10.1017/CBO9781139022262.010
Available formats
×

Save book to Google Drive

To save content items to your account, please confirm that you agree to abide by our usage policies. If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account. Find out more about saving content to Google Drive.

  • Input and output streams
  • David Yevick, University of Waterloo, Ontario
  • Book: A Short Course in Computational Science and Engineering
  • Online publication: 05 June 2012
  • Chapter DOI: https://doi.org/10.1017/CBO9781139022262.010
Available formats
×