C++ standard input and output streams

                                             
 
                The C++ language system defines a huge class library for realizing data input and output, including the main classes ios, istream, ostream, ifstream, ofstream, fstream, ofstream, fstream, istrstream, ostrstream, strstream, etc., of which ios is the foundation kind.
Three ways to set input and output formats in C++

        (1) Input/output stream enumeration constants;
            The access method is very simple, as long as each enumeration type constant is prefixed with iOS::, the corresponding format can be set. There are 13 enumeration constants that we commonly use, namely skipws, left, right, internal, dec, oct, hex, showbase, showpoint, uppercase, showpos, scientific, fixed.
        (2) Internal functions of input/output streams;
              Some of the internal functions for input/output stream format control are given below:
         Function form Function description
        int bad() returns a non-zero value when the operation fails
        int eof( ) returns a non-zero value when the end-of-file character at the end of the stream is read
        int fail( ) returns a non-zero value when the operation fails
        int clear( ) clears the flag states corresponding to bad, eof, and fail, returns them to normal state 0, and makes the good flag 1 

        int p recision( ) returns the floating-point output precision, that is, the number of significant digits of the output
              ps: The examples here are not complete, and there are more to be added.
        (3) Input/output stream format control operators;
            In an equally simple way of using data input/output format control, you need to add the header file #include<iomanip>. You don't need to call member functions to use these operators, just use them as output/input objects of the insertion operator << or >>.
            Contains dec,oct,hex,ws,endl,ends,flush,setiosflags(long f)......
Example 1: Simply write a program: input a decimal number from the keyboard, convert the number into decimal, octal, and hexadecimal respectively and output it to the screen.
#include <iostream>
#include <iomanip>
using namespace std;


intmain()
{
    int n = 0;
    cout << "input a decimal number  " ;
    cin >> n;

    cout.setf(ios::dec);
    cout << "you input decimal number is " << n << endl;
    cout.unsetf(ios::dec);//Clear decimal format

    cout.setf(ios::oct);
    cout << "converted to octal number is " << n << endl;
    cout.unsetf(ios::oct);//Clear octal format

    cout.setf(ios::hex);
    cout << "converted to hex number is " << n << endl;
    cout.unsetf(ios::hex);//Clear hexadecimal format
    return 0;
}

The output is as follows:
[root@localhost review]# vim demo1.c
[root@localhost review]# g++ demo1.c -o demo1
[root@localhost review]# ./demo1
input a decimal number  456
you input decimal number is 456
converted to octal number is 710
converted to hex number is 1c8
Example 2:
****************************************************
Funcion List: Input a char type number and convert it to decimal, octal, hexadecimal number display
*****************************************************/
#include <iostream>
using namespace std;
#include <iomanip>

intmain()
{
    char n;
    cout << "please input a character:";
    cin >> n;

    cout << "the decimal digital number is" << dec << (static_cast<int>(n))<<endl;

    cout <<"convertrd to octal number is" << oct << (static_cast<int>(n))<<endl;
    cout << "converted to hexdecital number is" << hex << (static_cast<int>(n))<<endl;
    return 0;
}

The output is as follows:
[root@localhost review]# vim demo2.cpp
[root@localhost review]# g++ demo2.cpp -o demo2
[root@localhost review]# ./demo2
please input a character:a
the decimal digital number is97
convertrd to octal number is141
converted to hexdecital number is61

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324649522&siteId=291194637