目录
- 1. setw manipulator(“设置域宽”控制符)
- 2. setprecision manipulator(“设置浮点精度”控制符)
- 3. setfill manipulator(“设置填充字符”控制符)
- 4. Formatting Output in File Operation(在文件操作中格式化输入/输出)
- 5.小练习
1、格式化输出
1. setw manipulator(“设置域宽”控制符)
要包含头文件
setw(n) 设置域宽,即数据所占的总字符数
std::cout << std::setw(3) << 'a'
<< std::endl;
输出:
_ _a
setw()控制符只对其后输出的第一个数据有效
std::cout << std::setw(5) << 'a'
<< 'b' << std::endl;
输出:
_ _ _ _ab
setw()的默认为setw(0),按实际输出。
如果输出的数值占用的宽度超过setw(int n)设置的宽度,则按实际宽度输出。
float f=0.12345;
std::cout << std::setw(3) << f
<< std::endl;
输出:
0.12345
2. setprecision manipulator(“设置浮点精度”控制符)
setprecision(int n)
(1) 控制显示浮点数的有效位
(2) n代表数字,总位数,不包括小数点
using namespace std;
int main() {
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;
}
VS效果:
2.42857
2.42857
2
2.4
2.43
2.42857
2.4285715
3. setfill manipulator(“设置填充字符”控制符)
setfill©
设置填充字符,即“<<"符号后面的数据长度小于域宽时,使用什么字符进行填充。
std::cout << std::setfill('*')
<< std::setw(5) << 'a'
<< std::endl;
输出:
****a
4. Formatting Output in File Operation(在文件操作中格式化输入/输出)
5.小练习
本部分展示内容如下;
任务1:展示setw和setfill
1、setw只对紧跟随其后的数据起作用
2、setfill指定填充字符
任务2:展示setprecision、fixed、showpoint、left、right
任务3:展示hexfloat
using std::cout;
using std::endl;
int main()
{
//任务1:展示setw和setfill
//cout << std::setw(4) << std::setfill('#') << "a";
cout << std::setfill('#');
for (int i = 0;i < 5;i++)
{
cout << std::setw(i+2) << ' ' << endl;
}
//任务2:展示setprecision、fixed、showpoint、left、right
double pi = 3.1415926535897;
cout << std::setprecision(6) << pi << endl;
//定点数代表了小数点后几位
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;
//任务3:展示hexfloat
cout << std::hexfloat << y << endl;
cout << std::defaultfloat;
cout << y << endl;
cout << std::showpoint << y << endl;
return 0;
}
2、用于输入/输出流的函数
1. getline()
'>>'运算符用空格分隔数据
对于文件内容:
Li Lei#Han Meimei#Adam
如下代码只能读入“Li”
ifstream input("name.txt");
std::string name;
input >> name;
如果用成员函数getline(char* buf, int size, char delimiter)读LiLei:
constexpr int SIZE{ 40 };
std::array<char , SIZE> name{};
while (!input.eof()) {// not end of file
input.getline(&name[ 0 ] , SIZE , '#');
std::cout << &name[ 0 ] << std::endl;
}
如果用非成员函数getline(istream& is, string& str, char delimiter)读LiLei:
std::string name2{};
while (!input.eof()) {
std::getline(input, name2, '#');
std::cout << n << std::endl;
}
2. get() and put()
get: read a character
//这一种需要将int类型强制转换为char类型
//char c = static_cast<char>(in.get());
int istream::get();
//char c; in.get(c);
istream& get (char& c);
put write a character
ostream& put (char c);
3. flush()
将输出流缓存中的数据写入目标文件:
ostream& flush();
用法:
cout.flush(); // 其它输出流对象也可以调用 flush()
cout << "Hello" << std::flush; // 与endl类似作为manipulator的调用方式
4.getline()练习
本部分要展示的内容如下;
任务1:展示istream::getline函数的用法
任务2:展示std::getline函数的用法
using std::cout;
using std::endl;
using std::ifstream;
using std::string;
int main()
{
//打开文件
std::filesystem::path p{ "scores.txt" };
ifstream in{p};
if (!in)
{
cout << "Can't open file" << p << endl;
std::abort();
}
//任务1:istream::getline函数
constexpr int SIZE = 1024;
std::array<char, SIZE> buf; //&buf
while (!in.eof())
{
in.getline(&buf[0], SIZE, '#');
cout << &buf[0] << endl;
}
//由于上面的操作已经读到文件末尾,此时需要关闭重新打开文件
in.close();
in.open(p);
//任务2:std::getline函数的用法
std::string name1{""};
while (!in.eof())
{
std::getline(in,name1,'#');
cout << name1 << endl;
}
std::cin.get();
return 0;
}
效果:
默认情况下,getline函数使用换行符作为分隔符