Setting and Resetting the Format State via Member Function flags - C++ File Stream

C++ examples for File Stream:cout

Description

Setting and Resetting the Format State via Member Function flags

Demo Code

#include <iostream>
#include <iomanip>
int main(int argc, const char *argv[]) {
    int integerValue = 1000;
    double doubleValue = 0.09876543;

    // display flags value, int and double values (original format)
    std::cout << "The value of the flags variable is: " << std::cout.flags()
              << "\nPrint int and double in original format:\n"
              << integerValue << '\t' << doubleValue << std::endl
              << std::endl;/*  ww  w .  j ava 2 s. c  o m*/

    // use cout flags function to save original format
    std::ios_base::fmtflags originalFormat = std::cout.flags();
    std::cout << std::showbase << std::oct << std::scientific;  // change format

    // display flags value, int and double values (new format)
    std::cout << "The value of the flags variable is: " << std::cout.flags()
              << "\nPrint int and double in original format:\n"
              << integerValue << '\t' << doubleValue << std::endl
              << std::endl;

    std::cout.flags(originalFormat);  // restore format

    // display the flags value, int and double values (original format)
    std::cout << "The value of the flags variable is: " << std::cout.flags()
              << "\nPrint int and double in original format:\n"
              << integerValue << '\t' << doubleValue << std::endl
              << std::endl;

    return 0;
}

Result


Related Tutorials