[C++] Input and output stream ⑦ (cout standard output stream object | cout.write function | cout.width / cout.fill / cout.setf function)






1. cout.write function




1. Introduction to cout.write function


cout.write(const char* s, int n) Member function: This function writes the first n characters of a character array into the stream; for example: cout.write("Hello, world!", 6) will be output on the console "Hello," ;


The function prototype is as follows: The ptr parameter is a pointer to the character array to be written, and the num parameter is the number of characters to be written;

ostream& write (const char* ptr, int num);

pay attention :

The cout.write() function does not add any EOF termination characters, and its function is only to write the contents of the memory to the output stream intact;

When using the cout.write() function, you must ensure that the number of characters written does not exceed the size of the array to prevent out-of-bounds errors;

The cout.write() function is usually used for low-level I/O operations, such as when a byte stream needs to be processed directly; in advanced text processing, it is usually more convenient and safe to use the stream insertion operator <<


2. Code example - cout.write function


Execute cout.write("TomAndJerry", 7); code, the first 7 characters of the "TomAndJerry" string will be output, and the command line output result will be "TomAndJ";


Code example:

#include "iostream"
using namespace std;

int main() {
    
    

	// 输出 "Tom" 字符串
	cout << "Tom" << endl;

	// 输出字符串
	cout.write("TomAndJerry", 7);

	// 输出回车换行
	cout << endl;

	
	// 控制台暂停 , 按任意键继续向后执行
	system("pause");

	return 0;
};

Results of the :

Tom
TomAndJ
请按任意键继续. . .

Insert image description here





2. cout.width / cout.fill / cout.setf function




1. Function introduction


cout.width / cout.fill / cout.setf function introduction:

  • width(int n) member function: This function sets the width of the next output or input operation; it is usually used together with other format functions, such as setw; such as : cout << setw(5) << 123 will output "123" on the console, and the space in front makes the total width 5;
  • fill(char c) Member function: This function sets the characters used to fill extra space; for example: cout << setfill('*') << setw(5) << 123 will output "123" on the console, because < /span>* is used as filler character;
  • setf(fmtflags f, fmtflags val) Member function: This function sets the format flag f, and can optionally set the second parameter val; for example: cout.setf(ios::showbase) turns on the display of numerical values Cardinality;

2. Code examples


The length of the output string is 8 bytes. If the output characters are not enough, use * padding to set the display reference data. If it is hexadecimal, display 0x format style.

Set * filling effect, fill to between 0x and number,

The command line outputs 123, displayed in hexadecimal;

The corresponding hexadecimal value of 123 is 0x7b,


Code example:

#include "iostream"
using namespace std;

int main() {
    
    

	// 输出字符串长度为 8 字节
	cout.width(8);

	// 如果输出的字符不够 8 个 , 使用 * 填充
	cout.fill('*');

	// 设置显示基准数据 , 如果是 十六进制 显示 0x 格式样式
	cout.setf(ios::showbase);

	// 设置 * 填充效果 , 填充到 0x 和 数字之间
	cout.setf(ios::internal);

	// 命令行输出 123 , 十六进制显示
	cout << hex << 123 << endl;

	
	// 控制台暂停 , 按任意键继续向后执行
	system("pause");

	return 0;
};

Results of the :

0x****7b
请按任意键继续. . .

Insert image description here

Guess you like

Origin blog.csdn.net/han1202012/article/details/134911909
Recommended