Toggle Menu

Streams


Streams

A stream is an object that transports and formats characters of fixed width.

Stream Libraries

  • istream - input stream
  • ostream - output stream
  • iostream - both input and output stream. Has the following children libararies: ifstream, ofstream, fstream, istringstream, ostringstream, stringstream

Stream Operators

  • Inserter Operator <<
  • Extractor Operator >>
  • Parse information expected by destination object according to its type

Console I/O

  1. Output with cout: outputs text to stdout. Used with inserter operator <<. Allows for chaining of strings/variables by adding inserters and the additional strings/variables to the right side. endl represents a newline character.
  2. Input with cin - reads stdin and stores input value into specified variable. Used wth extractor operator >>. Only reads up to a whitespace character (space, tab, newline)
  3. Output with cerr - sends its output to stderr. Allows a way to distinguish normal output and error messages. Works in the same way as cout.

Formatting Output

The output format can also be formatted. For example, if we wanted all floats to be represented with 2 decimals places, we add the following statements:
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
Any following cout statements that follow will output floats rounded to 2 decimal places.

File I/O

Similar to console I/O, programs can do file I/O to perform reading and writing to files.

Opening a text file

#include <fstream>
using namespace std;
ifstream inputStream;
inputStream.open("player.txt");
assure(inputStream, "player.txt");  // ensures that file is successfully opened
inputStream >> score >> firstName >> lastName;
Another way to open a file is by ifstream in ('player.txt');

Input Operations

  • cin.get(char &ch) - Puts the next input character in the variable ch. Returns an integer value, which is zero if it encountered a problem (e.g. end of file).
  • cin.getline(char *buffer, int length) - Reads characters into the string buffer until it reaches the length specified or a newline character. Stores a null character ('\0') after the last character read.
  • cin.read(char *buffer, int n) - Reads n bytes (or until the end of the file) from the stream into the buffer.
  • cin.ignore(int n) - Removes the next n characters (or until end of file) from the input stream. If no argument is given, skips until the next newline character.
  • cin.clear(iostate state) - Sets stream's error state flags to specified state (goodbit by default, which "clears" error flags).
  • cin.fail() - Returns true if the last cin command failed (e.g. if entered value does not fit variable). To reset the stream after an error:
    if (cin.fail()) {
        cin.clear(); // clears error flags
        cin.ignore(); // ignores "bad" character
    }
  • cin.bad() - Returns true if the badbit error state flag is set.
  • cin.eof() - Returns true if the eofbit error state flag is set.
  • cin.fail() - Returns true if badbit and/or failbit error state flags are set.
  • cin.good() - Returns true if none of the error state flags (badbit, failbit, eofbit) are set (i.e. state flag is goodbit)

Stream Error State Flags

Error state flags are used to keep track of the stream's state.
  • eofbit - end-of-file is reached
  • failbit - last input operation failed due to internal logic
  • badbit - error due to failure of an I/O operation on the stream buffer
  • goodbit - no error / absence of above error state flags

String <=> Integer

stringstream can be used to convert strings to and from integers.

String to Integer

#include <sstream>
#include <string>
using namespace std;
string s = "42 30";
istringstream iss (s);
int x, y;
iss >> x >> y;

Integer to String

ostringstream oss;
oss << 23 << "+" << 124;
string s = oss.str();