STREAM MANIPULATIONS:


Stream manipulators in C++ are operators utilized for formatting output. They empower programmers to control the presentation of data according to their requirements. Until now, the formatting of input and output information has been governed by the default settings of the C++ Input/Output system.

However, there are two ways to precisely control data formatting: using member functions of the ios class or employing special functions known as manipulators. Let's delve into formatting using the iosmember functions.

C++ provides built-in manipulator functions within various libraries. These libraries can be included in our programs to leverage these functions:

<iostream>

Provides fundamental functionalities for stream Input/Output in C++.


<iomanip>

Contains utilities for performing formatted Input/Output with parameterized stream manipulation.


<fstream>

Deals with user-controlled file processing operations.


<strstream>

Facilitates in-memory formatting similar to file processing, but operations are performed on character arrays instead of files.


<stdiostream>

For programs that combine C and C++ styles of Input/Output.

These manipulators are applicable to both input and output streams, although some may only affect either output or input streams.

boolalphaAlphanumeric bool Values:
  • This sets the boolalphaformat flag for the stream. When activated, bool values are represented as either "true" or "false" instead of integral values.
  • This flag can be disabled with the noboolalphamanipulator.

showbaseShow Numerical Base Prefixes:
  • Activates the showbaseformat flag for the stream. It prefixes numerical integer values with appropriate base indicators like "0x" for hexadecimal values and "0" for octal values.
  • This can be turned off with the noshowbasemanipulator.

showpointShow Decimal Point:
  • Sets the showpointformat flag for the stream. It ensures that the decimal point is always written for floating-point values, even if the decimal part is zero.
  • The noshowpointmanipulator disables this behavior.

showposShow Positive Signs:
  • Enables the showposformat flag, which adds a plus sign (+) before every non-negative numerical value.
  • noshowposmanipulator turns off this behavior.

skipwsSkip White Spaces:
  • This flag, when set, causes leading whitespace characters to be ignored during input operations.
  • noskipwsmanipulator disables this behavior.

unitbufFlush Buffer After Insertions:
  • Activates the unitbufflag, which flushes the buffer after each insertion operation.
  • nounitbufmanipulator disables this, preventing flushes after every insertion.

uppercaseGenerate Upper-Case Letters:
  • When set, this flag instructs the stream to use uppercase letters instead of lowercase in representations like hexadecimal values.
  • nouppercasemanipulator reverses this behavior.

Let's explore each of these manipulators with examples:

CPP Copy to Clipboard  
#include <iostream>
#include <sstream>
#include <ios>
#include <fstream>
using namespace std;

int main() {
    // boolalpha & noboolalpha
    bool b = true;
    cout << "boolalpha: " << boolalpha << b << endl;
    cout << "noboolalpha: " << noboolalpha << b << endl;

    // showbase & noshowbase
    int x = 15;
    cout << hex << "\nshowbase: " << showbase << x << endl;
    cout << hex << "noshowbase: " << noshowbase << x << endl;

    // showpoint & noshowpoint
    double a = 30, s = 10000.0, pi = 3.1416;
    cout.precision(5);
    cout << "\nShowpoint: " << showpoint << a << '\t' << s << '\t' << pi << '\n';
    cout << "noshowpoint: " << noshowpoint << a << '\t' << s << '\t' << pi << '\n';

    // showpos & noshowpos
    int p = 1, z = 0, c = -1;
    cout << "\nshowpos: " << showpos << p << '\t' << z << '\t' << c << endl;
    cout << "noshowpos: " << noshowpos << p << '\t' << z << '\t' << c << endl;

    // skipws & noskipws
    char wa, wb, wc;
    istringstream iss(" 123");
    iss >> skipws >> wa >> wb >> wc;
    cout << "\nSkipws: " << wa << wb << wc << '\n';

    iss.seekg(0);
    iss >> noskipws >> wa >> wb >> wc;
    cout << "noskipws: " << wa << wb << wc << '\n';

    // unitbuf & nounitbuf
    ofstream outfile("test.txt");
    outfile << unitbuf << "Test " << "file" << '\n'; // flushed three times
    outfile.close();

    // uppercase & nouppercase
    cout << showbase << hex;
    cout << "\nuppercase: " << uppercase << 77 << '\n';
    cout << "nouppercase: " << nouppercase << 77 << '\n';

    return 0;
}

These flags determine how integer values are represented in the stream: in decimal, hexadecimal, or octal form.

  • decUse Decimal Base.
  • hexUse Hexadecimal Base.
  • octUse Octal Base.
CPP Copy to Clipboard  
#include <iostream>
using namespace std;

int main() {
    int n = 70;
    cout << "dec: " << dec << n << '\n';  // dec
    cout << "hex: " << hex << n << '\n';  // hex
    cout << "oct: " << oct << n << '\n';  // oct

    return 0;
}

These flags control how floating-point values are represented in the stream: in fixed or scientific notation.

  • fixedUse Fixed Floating-Point Notation.
  • scientificUse Scientific Floating-Point Notation.
CPP Copy to Clipboard  
#include <iostream>
using namespace std;

int main() {
    double a = 3.1415926534, b = 2006.0, c = 1.0e-10;
    cout.precision(5);

    cout << "default:\n";
    cout << a << '\n' << b << '\n' << c << '\n';

    cout << "\nfixed:\n" << fixed;
    cout << a << '\n' << b << '\n' << c << '\n';

    cout << "\nscientific:\n" << scientific;
    cout << a << '\n' << b << '\n' << c << '\n';

    return 0;
}
wsExtract Whitespaces:
  • Extracts whitespace characters until a non-whitespace character is encountered.
CPP Copy to Clipboard  
#include <iostream>
#include <sstream>
using namespace std;

int main() {
    char a[10], b[10];

    istringstream iss("one \n \t two");
    iss >> noskipws;
    iss >> a >> ws >> b;
    cout << a << ", " << b << '\n';

    return 0;
}
  • endlInsert Newline and Flush.
  • endsInsert Null Character.
  • flushFlush Stream Buffer.
CPP Copy to Clipboard  
#include <iostream>
#include <fstream>
using namespace std;

int main() {
    // endl
    int a = 100;
    double b = 3.14;
    cout << "endl: " << endl;
    cout << a;
    cout << endl; // Manipulator inserted alone
    cout << b << endl << a * b; // Manipulator in concatenated insertion
    endl(cout); // endl called as a regular function

    // Flush test.txt 100 times
    ofstream outfile("test.txt");
    for (int n = 0; n < 100; n++)
        outfile << n << flush;
    outfile.close();

    return 0;
}
  • setiosflagsSet Format Flags.
  • resetiosflagsReset Format Flags.
  • setbaseSet Basefield Flag.
  • setfillSet Fill Character.
  • setprecisionSet Decimal Precision.
  • setwSet Field Width.
CPP Copy to Clipboard  
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
    // setiosflags
    cout << "setiosflags: " << endl;
    cout << hex;
    cout << setiosflags(ios::showbase | ios::uppercase);
    cout << 100 << endl;

    // resetiosflags
    cout << "\nresetiosflags: " << endl;
    cout << hex << setiosflags(ios::showbase);
    cout << 100 << endl;
    cout << resetiosflags(ios::showbase) << 100 << endl;

    // setbase
    cout << "\nsetbase: " << endl;
    cout << setbase(16);
    cout << 110 << endl;

    // setfill
    cout << "\nsetfill: " << endl;
    cout << setfill('x') << setw(10);
    cout << 77 << endl;

    // setprecision
    cout << "\nsetprecision: " << endl;
    double f = 3.14159;
    cout << setprecision(9) << f << '\n';
    cout << fixed;
    cout << setprecision(9) << f << '\n';

    // setw
    cout << "\nsetw: " << endl;
    cout << setw(10);
    cout << 77 << endl;

    return 0;
}