C + + language entry file

(1) Flow

I flow

Definition: the transfer of data from one object to another.

Function: standard input and output + file processing

Classification:

  • The text stream is a string of ASCII characters
  • Binary stream a string of binary

II Flow type

1. Standard I/O flow

  • ios is an abstract class
  • ostream is a class of cout, clog and cerr
  • istream is a class of cin

2. File stream type

  • ifstream reads data from a file
  • ofstream writes data to a file
  • iofstream file reading and writing data

3. Character stream type

  • istringstream reads data from string
  • ostringstream writes data to string
  • iostringstream reads and writes string data

III Stream object

Generally, standard I/O stream objects are global objects that do not need to be defined, while file stream objects and character stream objects need to be user-defined.  

There are four standard I/O flow objects:

NO.Global flow objectnamecache
1coutStandard output streamWith cache
2cinStandard input streamWith cache
3clogStandard log streamWith cache
4cerrStandard error flowNo cache

Stream objects usually cannot be copied.

IV Stream object state

The flow object state must be in one of the following four states at a certain time.  

NO.statemeaning
1good()The previous stream operation succeeded
2eof()To end of input / end of file
3fail()An unexpected event occurred (read failed)
4bad()Unexpected serious event (disk read failure)

V I/O operation

There are five main I/O operations:

  • Input operation: in > > x or getline(in,s)
  • Output operation: out < < x
  • Operator
  • Flow state
  • format

Output stream default settings

typeBase systemwidthalignmentfillaccuracy
integerdecimal system0Right alignSpace1
real numberdecimal system0Right alignSpace6 digits
character string-0Right alignSpaceActual length of string

Output control format of stream

1. Alignment

Flag (member function)Manipulator (operator)effect
ios::left leftbe at the left side
ios::right rightbe at the right
ios::internalinternalFill after output symbol or hexadecimal

2. Width setting (not sustainable)

Flag (member function)Manipulator (operator)
width()setw() header file iomanip
#include <iostream>
using namespace std;
int main(){
    //Member function
    int n = -11;
    cout.width(6);
    cout.flags(ios::right);
    cout << n << endl;

    //Operator
    int m = -21;
    cout << setw(7) << right << m << endl;

    //blend
    cout.width(5); cout << right << m << endl;
}

3. Integer output format

Flag (member function)Manipulator (operator)effect
ios::decdecdecimal system
ios::octoctoctal number system
ios::hexhexhexadecimal
ios::uppercaseuppercaseOutput hex using uppercase
ios::showbaseshowbaseOutput characters with base
#include <iostream>
using namespace std;
int main(){
    //Member function:
    int n = 11;
    cout.flags(ios::showbase|ios::oct);
    cout << n << endl;

    //Operator:
    int m = 11;
    cout << showbase << oct << m << endl;
}

Output: 013
            013

4. Floating point number output format

① How many digits are reserved by default (floating point format)

//Member function:
cout.precision(5);
cout << a << endl;
//Operator:
cout << setprecision(5)
     << a << endl;

② the maximum number of digits after the decimal point (fixed-point number method)

#include <iomanip>

//Member function:
cout.flags(ios::fixed);
cout << a << endl;
//Operator:
cout << fixed << a << endl;

③ floating point number output by scientific counting method

//Member function:
cout.flags(ios::scientific);
cout << a << endl;
//Operator:
cout << scientific << a << endl;

5. Boolean type

bool a = true;
//Output: 1
cout << a << endl;

//Output: true
cout << boolalpha << a << endl; //Operator

cout.flags(ios::boolalpha); //Member function
cout << a << endl; 

6. Other types

When showpos outputs decimal 0 or integer, it is marked with + - sign

int a = 100;
int b = -1;
//Operator:
cout << showpos << a << b << endl;
//Member function:
cout.setf(ios::showpos);
cout << a << b << endl;

Output: + 100-1

showpos is very suitable for complex imaginary part a+bi.

(2) Character stream

Header file to use: #include < ssstream >

Define your own stream object and use string object instead of character array to avoid the danger of buffer overflow. Moreover, the types of the incoming parameters and the target object are automatically derived, and there is no danger even if incorrect formatter is used.

#include <iostream>  
#include <sstream>
using namespace std;
int main () {
	ostringstream oss; //Define output string 
	oss << 123 << "abc" << 3.1415 << endl;//The upper stream can also be used 
	string s = oss.str(); //Define it as a string
	cout << s; 
	
	istringstream iss("123 abc 3.14");//Define input string 
	int n;
	string str;
	float f;
	iss >> n >> str >> f;
	cout << n << str << f << endl;
}

(3) File operation

Reading and writing:

#include <iostream> 
#include <fstream>
#include <string>
using namespace std;
int main () {
	string s;
	cin >> s;//Output to file
	
	ofstream of("./test",ios::app);//The output file path app is append 
	if(of){ //Determine whether the file is open 
		of << s;
		of.close();//Turn it off in advance 
	}
	
	ifstream ifs("./test");//input 
	string t;
	if(ifs){
		ifs >> t;
		ifs.close();
	}
	cout << t << endl;
}

 

Keywords: C++

Added by ridster on Sat, 08 Jan 2022 04:25:02 +0200