C++ IOStreams

The iostream library is C++‘s primary input/output mechanism. It replaces C’s printf/scanf with type-safe, extensible stream objects. The key insight: << and >> are overloaded operators — they have been given new meanings for stream types.

Includes and Setup

#include <iostream>      // cout, cin, endl, manipulators
#include <fstream>       // ifstream, ofstream (includes iostream automatically)
#include <string>        // for getline target
using namespace std;

Console Output: cout and <<

cout = “console output” — a global object defined by the iostream library.
<< = “send to” (overloaded from bitwise left shift)

cout << "Hello, World! I am "
     << 8 << " Today!" << endl;
  • Arguments chained left to right
  • endl = outputs \n AND flushes the buffer (use "\n" for newline without flush)
  • The << operator is overloaded to accept many types; cout “figures out” what to do

Output Manipulators

Manipulators change stream state — they don’t print, they affect subsequent output:

cout << dec << 15 << endl;   // 15  (decimal — default)
cout << oct << 15 << endl;   // 17  (octal)
cout << hex << 15 << endl;   // f   (hexadecimal)
cout << 3.14159 << endl;     // floating-point: auto-formatted by compiler
cout << char(27) << endl;    // cast ASCII 27 to char → sends escape character

Console Input: cin and >>

cin = “console input” — reads from standard input (normally keyboard, but can be redirected).
>> = “read into” — waits for input matching the argument’s type.

int number;
cout << "Enter a decimal number: ";
cin >> number;
cout << "value in octal = 0" << oct << number << endl;
cout << "value in hex = 0x" << hex << number << endl;

Character Arrays

Text in double quotes is a character array (not a C++ string). The compiler:

  • Stores the ASCII value of each character
  • Automatically appends a null terminator (0) to mark the end

Escape sequences:

SequenceMeaning
\nNewline
\tTab
\\Backslash
\bBackspace

Character Array Concatenation (Preprocessor Feature)

Adjacent quoted strings with no punctuation between them are merged by the preprocessor:

cout << "This is far too long to put on a "
        "single line but it can be broken up "
        "with no ill effects.\n";

This is purely a preprocessor feature; useful for wide code listings.

File I/O: ifstream and ofstream

#include <fstream>
ifstream in("filename.txt");    // behaves like cin
ofstream out("output.txt");     // behaves like cout

Once opened, use the same << and >> operators as console I/O. The stream abstraction unifies file, console, and string I/O.

Reading Lines with getline()

string s;
while(getline(in, s)) {         // reads one line; discards newline char
    out << s << "\n";            // must add newline back manually
}
  • getline() returns a value that is “true” while a line was successfully read, “false” at EOF
  • Discard the newline — it is not stored in the string
  • Use in a while condition to read until EOF

Reading Entire File into a String

ifstream in("file.cpp");
string s, line;
while(getline(in, line))
    s += line + "\n";
cout << s;

string grows automatically; you never need to pre-allocate.

Calling Other Programs

#include <cstdlib>
system("ls -la");    // passes string to OS shell; control returns after

Demonstrates C library reuse: the entire C standard library is available in C++ without changes.

graph TD
    A[iostream library] --> B[cout: console output]
    A --> C[cin: console input]
    A --> D[endl: newline + flush]
    A --> E[Manipulators: dec/oct/hex]
    F[fstream library] --> G[ifstream: file input, like cin]
    F --> H[ofstream: file output, like cout]
    G --> I[getline: read one line into string]
    B --> J[operator << overloaded: send to]
    C --> K[operator >> overloaded: read into]

Key Points

  • << is the “send to” operator for output streams; >> is “read from” for input streams — both are overloaded
  • endl flushes the buffer; "\n" only inserts a newline (prefer "\n" in loops for performance)
  • char(27) sends the character whose ASCII value is 27 — useful for escape codes
  • getline() discards the terminating newline; add "\n" back when writing to output
  • <fstream> includes <iostream> automatically, but include both explicitly for clarity
  • using namespace std; in a .cpp file is fine; never put it in a header file