directory

  • 1. Format output
    • 1. Setw manipulator(” Set range “controller)
    • 2. Setprecision manipulator(” Set floating point accuracy “controller)
    • 3. Setfill manipulator(” setfill character “controller)
    • Formatting Output in File Operation
    • 5. Small practice
  • 2. Functions for input/output streams
    • 1. getline()
    • 2. get() and put()
    • 3. flush()
    • 4. Getline () practice

1. Format output

1. Setw manipulator(” Set range “controller)

To include header files setw(n) sets the field width, that is, the total number of characters in the data

std::cout << std::setw(3) < <'a'<< std::endl; Output: _ _aCopy the code

The setw() control is valid only for the first data output after it

std::cout << std::setw(5) < <'a'
            << 'b'<< std::endl; Output: _ _ _ _abCopy the code

Setw () defaults to setw(0). If the output value occupies more than the width set by setw(int n), the output is the actual width.

float f=0.12345;
 std::cout << std::setw(3) << f << std::endl; Output:0.12345
Copy the code

2. Setprecision manipulator(” Set floating point accuracy “controller)

setprecision(int n)

(1) Control display of floating point number significant bits (2) N represents the number, total number of digits, excluding the decimal point

#include <iostream>
#include <iomanip>
using namespace std;

int main(a) {
  float f = 17 / 7.0;
  cout <<                    f << endl;
  cout << setprecision(0) << f << endl;
  cout << setprecision(1) << f << endl;
  cout << setprecision(2) << f << endl;
  cout << setprecision(3) << f << endl;
  cout << setprecision(6) << f << endl;
  cout << setprecision(8) << f << endl;
  return 0;

}
Copy the code

VS:

2.42857

2.42857

2

2.4

2.43

2.42857

2.4285715

3. Setfill manipulator(” setfill character “controller)

Setfill © Sets the fill character, that is, what character to use to fill the data after the “<<” symbol if the length of the data is less than the width of the field.

std::cout << std::setfill(The '*') 
           << std::setw(5) < <'a'<< std::endl; Output: * * * * aCopy the code

Formatting Output in File Operation

5. Small practice

The contents of this section are as follows; Task 1: show setw and setfill 1, setw only works on data immediately following 2, setfill specifies the fill character task 2: show setPrecision, Fixed, Showpoint, left, right task 3: Show hexfloat

#include <iostream>
#include <iomanip>

using std::cout;
using std::endl;
int main(a)
{
	Task 1: Show setw and setfill
	//cout << std::setw(4) << std::setfill('#') << "a";
	cout << std::setfill(The '#');
	for (int i = 0; i <5; i++) { cout << std::setw(i+2) < <' ' << endl;
	}

	// Task 2: Display setPrecision, Fixed, showpoint, left, right
	double pi = 3.1415926535897;

	cout << std::setprecision(6) << pi << endl;
	// Fixed point number represents the number of decimal places
	cout << std::setprecision(6) << std::fixed << pi << endl;

	double y = 3.0;
	cout << y << endl;
	cout << std::showpoint << y << endl;
	cout << std::setw(20) << std::left << pi << endl;
	cout << std::setw(20) << std::right << pi << endl;


	// Task 3: Show hexfloat

	cout << std::hexfloat << y << endl;
	cout << std::defaultfloat;
	cout << y << endl;
	cout << std::showpoint << y << endl;
	return 0;
}
Copy the code

2. Functions for input/output streams

1. getline()

The ‘>>’ operator separates data with Spaces

Li Lei#Han Meimei#Adam the following code can only be read as “Li”

ifstream input("name.txt");
std::string name;
input >> name;
Copy the code

If you use getline(char* buf, int size, char delimiter) to read LiLei:

constexpr int SIZE{ 40 };
std::array<char , SIZE> name{};
while(! input.eof()) {// not end of file
  input.getline(&name[ 0 ] , SIZE , The '#');
  std::cout << &name[ 0 ] << std::endl;
}
Copy the code

If you use the non-member function getline(istream& is, string& STR, char delimiter) to read LiLei:

std::string name2{};
while(! input.eof()) {
  std::getline(input, name2, The '#');
  std::cout << n << std::endl;
}
Copy the code

2. get() and put()

get: read a character

// This one casts int to char
//char c = static_cast<char>(in.get());
int istream::get(a);
//char c; in.get(c);
istream& get (char& c);
Copy the code

put write a character

ostream& put (char c);
Copy the code

3. flush()

Write data from output stream cache to target file:

ostream& flush(a);
Copy the code

Usage:

cout.flush(a);// Other output stream objects can also call flush()
cout << "Hello" << std::flush; // Similar to endL as a call method for a manipulator
Copy the code

4. Getline () practice

The contents of this section are as follows; Task 1: show the use of the istream::getline function Task 2: show the use of STD ::getline function

#include <iostream>
#include <fstream>
#include <array>
#include <string>
#include <filesystem>
using std::cout;
using std::endl;
using std::ifstream;
using std::string;

int main(a)
{
	// Open the file
	std::filesystem::path p{ "scores.txt" };
	ifstream in{p};
	if(! in) { cout <<"Can't open file" << p << endl;
		std::abort(a); }Task 1: istream::getline function
	constexpr int SIZE = 1024;
	std::array<char, SIZE> buf;	//&buf
	while(! in.eof())
	{
		in.getline(&buf[0], SIZE, The '#');
		cout << &buf[0] << endl;
	}
	// Close and reopen the file because the previous operation has reached the end of the file
	in.close(a); in.open(p);
	// Task 2: the use of STD ::getline
	std::string name1{""};
	while(! in.eof())
	{
		std::getline(in,name1,The '#');
		cout << name1 << endl;
	}
	std::cin.get(a);return 0;

}
Copy the code

Effect:



By default, the getLine function uses a newline delimiter