ios_base类会存储输出格式状态的信息(比如 输出间隔,小数点位数等) ios继承于ios_base ostream继承于ios
格式设置函数
hex()十六进制
oct()八进制
dec()十进制
abc()一万进制(骗你的)
使用的时候可以直接这样: cout << hex; 这样就设置输出为16进制的了
程序示例
#include <iostream>
int main()
{
using namespace std;
cout << "Enter an integer: ";
int n;
cin >> n;
cout << "n n*n\n";
cout << n << " " << n * n << " (decimal)\n";
cout << hex;
cout << n << " ";
cout << n * n << " (hexadecimal)\n";
cout << oct << n << " " << n * n << " (octal)\n";
dec(cout);
cout << n << " " << n * n << " (decimal)\n";
return 0;
}
调整字段宽度
width()返回当前字段宽度
width(int i ) 设置指端宽度(i个空格)并且返回以前的字段宽度
只影响下一个项目 也就是说 输出
cout.width(12)
cout << 12 << 22 << '#';
这时候 宽度只有在12的前面显示。然后就1222#了 它不会一直持续保持宽度 一次性的
width() 是成员函数
程序示例
#include <iostream>
int main()
{
using std::cout;
int w = cout.width(30);
cout << "default field width = " << w << ":\n";
cout.width(5);
cout << "N" << ':';
cout.width(8);
cout << "N * N" << ":\n";
for(long i = 1; i <= 100; i *= 10)
{
cout.width(5);
cout << i << ':';
cout.width(8);
cout << i * i << ":\n";
}
return 0;
}
字符填充
fill() 参数为指定用哪个字符填充未被使用的部分(只能是字符,不能使字符串)
#include <iostream>
int main()
{
using std::cout;
cout.fill('*');
const char* staff[2] = {"waldo Whipsnade", "Wilmarie Wooper"};
long bonus[2] = {900, 1350};
for (int i = 0; i < 2; i++)
{
cout << staff[i] << ": $";
cout.width(7);
cout << bonus[i] << "\n";
}
return 0;
}
fill()一直有效 不是一次性的
设置显示位数 precision() 是整体位数不是小数点位数哦
#include <iostream>
int main()
{
using std::cout;
float price1 = 20.40;
float price2 = 1.9 + 8.0 / 9.0;
cout << "\"Furry Friends\" is $" << price1 << "!\n";
cout << "\"Furry Friends\" is $" << price2 << "!\n";
cout.precision(2);
cout << "\"Furry Friends\" is $" << price1 << "!\n";
cout << "\"Furry Friends\" is $" << price2 << "!\n";
return 0;
}
setf()设置末尾的0和小数点
定义了多个空值显示的多个常量
cout.setf(ios_base::showpoint(静态常量)) 显示6位数(包含整数部分)并显示末尾的0
#include <iostream>
int main()
{
using std::cout;
using std::ios_base;
float price1 = 20.40;
float price2 = 1.9 + 8.0 / 9.0;
cout.setf(ios_base::showpoint);
cout << "\"Furry Friends\" is $" << price1 << "!\n";
cout << "\"Furry Friends\" is $" << price2 << "!\n";
cout.precision(2);
cout << "\"Furry Friends\" is $" << price1 << "!\n";
cout << "\"Furry Friends\" is $" << price2 << "!\n";
return 0;
}
第三行因为设置了位数 所以只能显示最前面的两位
每一位有特定的定义 不用去记哪一位的开关是啥作用的
setf()有两个原型
第一个 fmtflags setf(fmtflags)
程序示例
#include <iostream>
int main()
{
using std::cout;
using std::endl;
using std::ios_base;
int temperature = 63;
cout << "Today's water temperature: ";
// 显示+号 10进制
cout.setf(ios_base::showpos);
cout << temperature << endl;
cout << "Por our programming freinds, that's\n";
// 显示十六进制
cout << std::hex << temperature << endl;
// 十六进制大写并且显示C风格标识
cout.setf(ios_base::uppercase);
cout.setf(ios_base::showbase);
cout << "or\n";
cout << temperature << endl;
cout << "How " << true << "! oops -- How ";
// 显示布尔值
cout.setf(ios_base::boolalpha);
cout << true << "!\n";
return 0;
}
hex() 等于cout.setf(ios_base::hex, ios_base::basefield)
程序示例
#include <iostream>
#include <cmath>
int main()
{
using namespace std;
// 左对齐
cout.setf(ios_base::left, ios_base::adjustfield);
// 显示+号
cout.setf(ios_base::showpos);
// 显示小数点
cout.setf(ios_base::showpoint);
// 三位数
cout.precision(3);
// 科学计数法
ios_base::fmtflags old = cout.setf(ios_base::scientific, ios_base::floatfield);
cout << "Left Justification:\n";
long n;
for(n = 1; n <= 41; n += 10)
{
cout.width(4);
cout << n << "|";
cout.width(12);
cout << sqrt(double(n)) << "|\n";
}
//符号或前缀左对齐,值右对齐
cout.setf(ios_base::internal, ios_base::adjustfield);
// 科学计数法
cout.setf(old, ios_base::floatfield);
cout << "Internal Justification:\n";
for ( n = 1; n <41; n +=10)
{
cout.width(4);
cout << n << "|";
cout.width(12);
cout << sqrt(double(n)) << "|\n";
}
// 右对齐
cout.setf(ios_base::right, ios_base::adjustfield);
// 定点计数 非科学计数
cout.setf(ios_base::fixed, ios_base::floatfield);
cout << "Right Justification:\n";
for ( n = 1; n <41; n +=10)
{
cout.width(4);
cout << n << "|";
cout.width(12);
cout << sqrt(double(n)) << "|\n";
}
return 0;
}
结果
可以使用void unsetf(fmtflags mask)消除setf()的效果 mask位模式 指定哪个位恢复为0
浮点数默认的位模式是 cout.setf(0, ios_base::floatfield) 将floatfield中的两个位都设置成0
或者用cout.unsetf(ios_base::floatfield)
使用头文件 iomanip
3个常用的控制符
setprecision() 设置精度,接受1个整形参数定位精度数
setfill()接受一个char类型字符指定填充符
setw()接受一个指定字段宽度的整数参数
#include <iostream>
#include <cmath>
#include <iomanip>
int main()
{
using namespace std;
cout << fixed << right;
// 设置宽度
cout << setw(6) << "N" << setw(14) << "square root"
<< setw(15) << "fourth root\n";
double root;
for (int n = 10; n <= 100; n += 10)
{
root = sqrt(double(n));
// . ' ' 填充字符
cout << setw(6) << setfill('.') << n << setfill(' ')
// 指定精度
<< setw(12) << setprecision(3) << root
<< setw(14) << setprecision(4) << sqrt(root)
<< endl;
}
return 0;
}
完结 卧槽 最后一小节这么多内容的么
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
const int LIM = 20;
struct planet
{
char name[LIM];
double population;
double g;
};
const char* file = "planets.dat";
inline void eatline() {while (std::cin.get() != '\n') continue;}
int main()
{
using namespace std;
planet pl;
cout << fixed;
fstream finout;
// 读写
finout.open(file, ios_base::in | ios_base::out | ios_base::binary);
int ct = 0;
if (finout.is_open())
{
// 输出流指针移到文件首
finout.seekg(0);
cout << "Here are the current contents of the "
<< file << " file:\n";
// 如果有内容就先读取出来
while (finout.read((char*) &pl, sizeof pl))
{
cout << ct++ << ": " << setw(LIM) << pl.name << ": "
<< setprecision(0) << setw(12) << pl.population
<< setprecision(2) << setw(12) << pl.g << endl;
}
// 判断是否到文件尾
if (finout.eof())
finout.clear();
else{
cerr << "Error in reading " << file << ".\n";
exit(EXIT_FAILURE);
}
}
else
{
cerr << file << " could not be opened -- bye.\n";
exit(EXIT_FAILURE);
}
cout << "Enter the record number you wish to change: ";
long rec;
cin >> rec;
eatline();
if (rec < 0 || rec >= ct)
{
cerr << "Invalid record number -- bye\n";
exit(EXIT_FAILURE);
}
// 将输出指针移动到指定位置
streampos place = rec * sizeof pl;
finout.seekg(place);
if (finout.fail())
{
cerr << "Error on attempted seek\n";
exit(EXIT_FAILURE);
}
// 读取出指定位置的内容
finout.read((char*) &pl, sizeof pl);
cout << "Your selection:\n";
cout << rec << ": " << setw(LIM) <<pl.name << ": "
<< setprecision(0) << setw(12) << pl.population
<< setprecision(2) << setw(6) << pl.g << endl;
if(finout.eof())
finout.clear();
cout << "Enter planet name: ";
cin.get(pl.name, LIM);
eatline();
cout << "Enter planetary population: ";
cin >> pl.population;
cout << "Enter planet's acceleration of gravity: ";
cin >> pl.g;
// 将数据写入指定位置
finout.seekp(place);
finout.write((char *) &pl, sizeof pl) << flush;
if(finout.fail())
{
cerr << "Error on attempted write\n";
exit(EXIT_FAILURE);
}
ct = 0;
// 输出指针移动到文件首 然后把文件全部读取出来
finout.seekg(0);
cout << "Here are the new contents of the " << file
<< " file:\n";
while(finout.read((char *) &pl, sizeof pl))
{
cout << ct++ << ": " << setw(LIM) << pl.name << ": "
<< setprecision(0) << setw(12) << pl.population
<< setprecision(2) << setw(6) << pl.g << endl;
}
finout.close();
cout << "Done.\n";
return 0;
}