C++: Standard I/O stream

Standard I/O objects: cin, cout, cerr, clog

  • cout; //The global stream object outputs data to the display
  • cin; //cerr has no buffer, clog has buffer
  • cerr; //Standard error output data to the display
  • clog; //Standard log output data to the display

One, standard input stream

cin.get()

// Only one character can be read at a time

	char ch;
	while ((ch = cin.get()) != EOF){
    
    //EOF就是键盘Ctrl+z,常被作为结束的标志
		cout << ch << endl;
	}
/*
结果:
cin
c
i
n
*/
cin.get (one parameter)

//Read a character

	char ch2;
	cin.get(ch2); //读取一个字符
	cout << ch2 << endl;
/*
结果:
cin
c
*/
cin.get (three parameters)

//Can read strings

	char buf[256] = {
    
     0 };
	cin.get(buf, 256); //从缓冲区读一个字符串
	cout << buf;
/*
结果:
输入:cin.get()  111
输出:cin.get()  111
*/
cin.getline ()
	char buf[256] = {
    
     0 };
	cin.getline(buf, 256); //读取一行数据
	cout << buf;
/*
输入:cin.getline() cin.get()
输出:cin.getline() cin.get()
*/
cin.ignore()

Sometimes you only want to take a part of the buffer and discard the other part. This is the case with cin.ignore(), which is used as follows:

cin.ignore(int intExp, char chExp);

Among them, intExp is an integer expression or an integer value. This value represents the maximum number of characters ignored in a line, for example, intExp=100; there is also a parameter chExp, which is a character expression. Indicates that if a character value equal to chEXP is encountered, then ignore() is stopped. If a character with a value equal to chEXP is not encountered after ignore 100 characters, then ignore() must be stopped, so 100 is the maximum value ignored by ignore() Number of characters.

	char ch;
	cin.get(ch); //从缓冲区要数据 阻塞
	cout << ch << endl;
	cin.ignore(10); //忽略当前字符 从缓冲区取走了
	cin.get(ch);
	cout << ch << endl;
/*
结果:
输入:asdfghjklzxcvbnm
输出:a
	 c
*/
cin.peek ()

This function returns the next character in the input stream, but does not remove the character from the input stream-equivalent to just glance at the next character, so it is called peek.

cin.peek() will not skip spaces and carriage returns in the input stream. In the case where the input stream has ended, cin.peek() returns EOF.

When the format of the input data is different and the format needs to be pre-judged before deciding how to input, peek() can play a role.

cout << "请输入数组或者字符串:" << endl;
char ch;
ch = cin.peek(); //偷窥一下缓冲区,返回第一个字符
if (ch >= '0' && ch <= '9') {
    
    

	int number;
	cin >> number;
	cout << "您输入的是数字:" << number << endl;

}
else {
    
    
	char buf[256] = {
    
     0 };
	cin >> buf;
	cout << "您输入的是字符串:" << buf << endl;
}
/*
结果:
	请输入数组或者字符串:
	acdsf
	您输入的是字符串:acdsf
*/
cin.putback()

Iostream is a class with its own buffer. Every time you cin>>a, it reduces the buffer by one and puts it in a. Cin.putback(a) puts the content of a back into the buffer.

	cout << "请输入字符串或者数字:" << endl;
	char ch;
	cin.get(ch); //从缓冲区取走一个字符
	if (ch >= '0' && ch <= '9') {
    
    
		//ch放回到缓冲区
		cin.putback(ch);
		int number;
		cin >> number;
		cout << "您输入的是数字:" << number << endl;
	}
	else {
    
    
		cin.putback(ch);
		char buf[256] = {
    
     0 };
		cin >> buf;
		cout << buf << endl;
	}
/*
结果:
请输入字符串或者数字:
33333
您输入的是数字:33333
*/

Second, the standard output stream

cout.flush()

The content to be output will be stored in the buffer first, and the function of cout.flush() is to forcibly empty the data in the buffer. In this way, data will not be lost when the read-write stream is closed.

cout.put()

cout.put() is dedicated to adding a single character to the output stream buffer,

cout.write()

The write() member method is dedicated to adding the specified string to the output stream buffer, and beginners can simply understand it as outputting the specified string. The syntax format is as follows:

ostream&write(const char * s,streamsize n);

Among them, s is used to specify a character array or string with a length of at least n; n means the first n characters to be output.

	cout << "hello world" <<endl;
	//cout.flush();
	cout.put('h').put('e').put('l') << endl;
	cout.write("hello Zhaosi!", strlen("hello Zhaosi!"));
/*
结果:
hello world
hel
hello Zhaosi!
*/
cout.width()
  • The control character int width() will be used to adjust the width of the field. Because width is a member function, it must be called by the object. For example, cout.width() will display the current field width, the default is 0, and cout.width(3 ) Will set the field width to 3. Note that the way C++ accommodates fields is to assign just the right width to the field to accommodate the field, so the default field width in C++ is 0 to fit all fields.
  • The default alignment of width is right alignment, that is, if the field is not so wide in cout.width(12), a space will be filled on the left side of the field to achieve the width of 12 fields.
  • Also note that width only affects the next output after it is set. After the next field is output, subsequent fields are restored to the default value, such as cout.width(12); cout<<2<<3; then output At 2 it will be displayed with a width of 12 fields, but at 3 it will be displayed by default.
  • Int width() will return the value of the last field width when it is called.
cout.fill ()

The member function fill() can be used to change the character to be filled, such as cout.fill('*'), use * to fill the blank part.

But be careful: the fill function will always be effective after it is set, unless it is reset.

And cout.fill('*') will return the fill character before setting'*' '', so if you use cout<<cout.fill('*'), this space will be output

cout.setf(marker)

The function of cout.setf() is to control the output form by setting the format flag

  • 1. Use the control symbol to control the output format
    . The function of the control symbol is
    dec to set the base of an integer to 10
    hex to set the base of an integer to 16
    oct to set the base of an integer to 8
    setbase(n) to set the base of an integer to n (n can only be 16, 10 , One of 8)
    setfill© Set the fill character c, c can be a character constant or a character variable
    setprecision(n) Set the precision of a real number to n digits. When outputting as a general decimal decimal number, n represents a valid number. In fixed( When outputting in fixed decimal places) and scientific (exponential) forms, n is the number of decimal
    places.setw(n) sets the field width to n
    places.setiosflags(ios::fixed) sets floating-point numbers to be displayed with fixed decimal places.
    setiosflags(ios::scientific) Set the floating point number to be displayed in scientific notation (that is, exponential form).
    setiosflags(ios::left) The output data is left aligned.
    setiosflags(ios::right) The output data is aligned right.
    setiosflags(ios:: shipws) Ignore leading spaces.
    setiosflags(ios::uppercase) When outputting E in scientific notation and the letter X in hexadecimal, it is expressed in uppercase.
    setiosflags(ios::showpos) When outputting a positive number, it gives " +" sign.
    resetiosflags terminates the output format state that has been set, and the content should be specified in brackets.
  • 2. Use the member of the stream object to control the output format. The
    stream member function has the same function as the control symbol.
    precision(n) setprecision(n) sets the precision of the real number to n bits.
    width(n) setw(n) sets the field width to n position.
    fill the fill character set © © setfill C.
    setf () setiosflags () format the output state should be given in parentheses state format, the same contents of control characters in parentheses setiosflags content.
    ubsetf () resetiosflags () to terminate the set Output format status.
    cout.width(10);
    cout.setf(ios::hex);
  • 3. Set the format flag of the format status. Format flag
    function
    ios::left The output data is left aligned within the wide range of this field
    ios::right The output data is right aligned within the wide range of this field
    ios::internal The sign bit of the value is in the wide range of this field The inner left-justified, the value is right-justified, and the middle is filled with padding characters
    ios::dec Set the base of the integer to 10
    ios::oct Set the base of the integer to 8
    ios::hex Set the base of the integer to 16
    ios::showbase Force the output of an integer The base (octal system starts with 0, hexadecimal system starts with 0x)
    ios::showpoint forces the output of the floating point number and the mantissa 0
    ios::uppercase when outputting E in scientific notation and outputting letter X in hexadecimal , In uppercase,
    ios::showpos will give a "+" sign when outputting a positive number.
    ios::scientific Set floating point numbers to display in scientific notation (ie exponential form)
    ios::fixed Set floating point numbers to a fixed number of decimal places Display
    ios::unitbuf refresh all streams after each output
    ios::stdio clear stdout, stderr after each output
	//成员方法的方式
	int number = 10;
	cout << number << endl;
	cout.unsetf(ios::dec); //卸载当前默认的的10进制输出方式
	cout.setf(ios::oct); //八进制输出
	cout.setf(ios::showbase);
	cout << number << endl;
	cout.unsetf(ios::oct); //卸载8进制
	cout.setf(ios::hex);
	cout << number << endl;

	cout.width(10);
	cout.fill('*');
	cout.setf(ios::left);
	cout << number << endl;


	//通过控制符
	int number2 = 10;
	cout << hex
		<< setiosflags(ios::showbase)
		<< setw(10)
		<< setfill('*')
		<< setiosflags(ios::left)
		<< number2
		<< endl;
/*
结果:
10
012
0xa
0xa*******
0xa*******
*/

Guess you like

Origin blog.csdn.net/weixin_45341339/article/details/112970465