basicCppio.pdf - Basic C Stream I/O David Kieras EECS Dept...

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

View Full Document Right Arrow Icon
Basic C++ Stream I/O David Kieras, EECS Dept., Univ. of Michigan 12/29/2011 Stream Basics What's a stream? A stream is a popular concept for how to do input/output. Basically, a stream is a sequence of characters with functions to take characters out of one end, and put characters into the other end. In the case of input/output streams, one end of the stream is connected to a physical I/O device such as a keyboard or display. If it is a console output stream, your program puts characters into one end of the stream, and the display system takes characters out of the other and puts them on the screen. If it is a console input stream, the keyboard puts characters into one end of the stream, and your program takes characters out of the other and stores the results in variables in the program. If no characters are waiting in the input stream, your program must wait until you supply some by typing on the keyboard. File streams follow the same principle, except that the file system is attached to the other end of the stream. What are cin and cout ? Two streams exist to allow you to communicate with your "console" - the screen and keyboard setup that makes up the traditional computer user interface. These are the input stream cin (console input), which is connected to the keyboard, and the output stream cout (console output), which is connected to your display. These two streams are created when the system starts your program. To use cin and cout and the input and output operators for them, you have to tell the compiler about them by including the relevant declarations in the library header file: #include <iostream> using namespace std; // See the namespace handout for when this form is appropriate When your program is linked, the relevant library object modules must be included - usually this is automatic or a normal part of your compilation/linking process. The streams cin and cout are actually global variables (or global objects) that are declared in <iostream> and initialized during program startup. This document uses cin and cout for examples, but everything herein works for any other text input or output stream, such as one your program might create itself for disk file input/output. Stream Output How does the output operator work? The behavior of the output operator << resembles the C stdio 's printf function. The value of the variable is converted into a sequence of characters depending on the type of the variable and a set of formatting rules, and these characters are inserted into the output stream for transmission to the output device (thus << is also called the stream insertion operator). Additional items, called manipulators , can be output to control the output stream. For example: int int_var = 123; cout << int_var << endl; 1
Image of page 1

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

View Full Document Right Arrow Icon
The variable int_var is an integer containing the binary value for 123. The output operator interprets this bit pattern for an integer variable as meaning it should construct the sequence of characters '1' , '2', and '3' and insert them into the output stream. When the next output operator gets the manipulator endl
Image of page 2
Image of page 3
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