00 - C++ IO Background

00 - C++ IO Background - 9/1/10 C+ I/O - Background We...

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

View Full Document Right Arrow Icon
9/1/10 1 Background Material C++ I/O C++ I/O - Background We assume most of you know how to do I/O in C++. These notes are for those who would like a refresher course. Input/Output Streams A popular model for how input and output is done in computer systems is centered around the notion of a stream. A stream is just a sequence of data with functions to put data into one end, and take them out of the other. For example: keyboard program display program file program In C++, streams are unidirectional. Data is always passed through the stream in one direction. If you want to read and write data to the same file or device, you have to use two streams. Input/Output Streams In general, there are two kinds of stream data: characters and binary data . Characters are usually used for: Communicating between your program and a keyboard and/or screen. Reading and writing files, such as when you are editing a text file. In addition to text, files can contain arbitrary binary data. It is usually much more efficient to pass binary representations of things than equivalent character representations. However, compared to binary data, character streams are easier for humans to understand and debug. Input/Output Streams In general, there are two kinds of stream data: characters and binary data . Characters are usually used for: Communicating between your program and a keyboard or screen. Reading and writing files, such as when you are editing a text file. In addition to text, files can contain arbitrary binary data. It is usually much more efficient to pass binary representations of things than equivalent character representations. However, compared to binary data, character streams are easier for humans to understand and debug. As computers and networks get faster, and storage becomes cheaper, the efficiency argument becomes weaker. So we'll be talking about character streams here. Input/Output A brief note on Binary Streams Binary streams are useful for transferring data structures into and out of memory with lower overhead. For example, a 32-bit binary representation of an integer fits into four bytes, whereas a character representation of the number could take as much as 11 bytes. There are also the added costs of converting from characters to binary and back again. - 2 1 4 7 4 8 3 6 4 8 -2 32 -2147483648 8 0 0 0 0x Characters Binary
Background image of page 1

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

View Full DocumentRight Arrow Icon
9/1/10 2 Input/Output Streams The C++ (not the C) stream I/O model is designed to make it easy to input/output values of simple types (like ints and doubles) and strings delimited by whitespace. You also get a few controls to adjust things like the field width of a number to be output, but it's hard to do just about anything else. First, let’s look at stream output.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/07/2012 for the course EECS 280 taught by Professor Noble during the Fall '08 term at University of Michigan.

Page1 / 5

00 - C++ IO Background - 9/1/10 C+ I/O - Background We...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online