06--Tools for IO stream (The Second Wave)

来源:互联网 发布:手机打卡考勤软件 编辑:程序博客网 时间:2024/04/30 21:15
  1. 用流函数格式化输出
    对程序输出的布局进行调整称为对输出进行格式化。
    setf()的格式化标志(setf == set flags):
    Example:
    cout.setf(ios::fixed);    cout.setf(ios::showpoint);    cout.setf(ios::right);    cout.precision(2);    outStream.setf(ios::fixed);    outStream.setf(ios::showpoint);    outStream.setf(ios::right);    outStream.precision(2);
 (1). ios::fixed       如果设置这个标志,就不用e记数法来写浮点数(如果设置该标志,会自动取消ios::scientific标志); (2). ios::scientific    如果设置这个标志,就会用e记数法来写浮点数(如果设置该标志,会自动取消ios::fixed标志); (3). ios::showpoint   如果设置这个标志,就始终为浮点数显示小数点和尾随的0(如果没有设置这个标志,而且一个数字在小数点之后全是0,那么当这个数字输出时,就可能不会显示小数点和尾随的0); (4). ios::showpos    如果设置这个标志,正整数之前会输出一个正号; (5). ios::right    如果设置这个标志,而且通过调用成员函数width来指定某个域宽值,那么输出的下一项会定位在指定的那个域的右侧 (右对齐)。也就是说,在输出的项之前,会根据需要添加用于填充的空格(如果设置该表示会自动取消设置ios::left标志); (6). ios::left    如果设置这个标志,而且通过调用成员函数width来指定某个域宽值,那么输出的下一项会定位在指定的那个域的左侧 (左对齐)。也就是说,在输出的项之前,会根据需要添加用于填充的空格(如果设置该表示会自动取消设置ios::right标志);(7). precision(reservedBits)    每个输出流中都有一个precision的成员函数。precision函数调用只对调用中指定的那个流生效。 (8). width(Bits)         对width函数的调用值适用于下一个要输出的项。         cout<<"Start Now";         cout.width(4);         cout<<7<<endl;  w和7之间有3和空格,width函数的作用就是告诉流下一个输出项需要占多少个字符位置(即域宽)。  如果输出所需的字符位数目超过width函数调用所指定的数目,就自动补足缺少的字符位置。总之,输出项始终都会完整输出,不会被截断,不管width指定的参数是什么。

2. “标志”术语解释
为什么将setf的参数(比如ios::showpoint)称为标志?ios::这个奇怪的表示法究竟有何含义?
标志(flag)一词表示某些可以开和关的东西。在任何情况下,如果ios::showpoint标志被设置(也就是说将它作为setf的参数),调用setf函数的那个流就会具有在图6.5中的总结的行为。同样地,其它任何标志被设置(也就是说,将它作为setf的参数),调用setf函数的那个流就会具有以上特征。
对于ios::z这种奇怪的表示方法,ios是“输入或输出流”的简称,::的意思则是说:“::之后的术语的具体含义是以::之前的术语为背景的”。
3. 流作为函数实参
流可以作为函数的实参使用。唯一的限制就是函数的形参必须是传引用。流参数不能是一个传值参数。

   //说明对输出进行格式化的命令// 读取 rawData.txt文件中所有数字,然后才有美观合适,将// 数字写到屏幕和neat.txt文件中#include <iostream>#include <fstream>#include <iomanip>#include <cstdlib>using namespace std;void make_neat(ifstream& messyFile, ofstream& neatFile, int reservedBits, int fieldWidth);// preconditions: The streams of messyFile and neatFile have been linked to the file by function open()// posconditions: the file linked with messyFile is printed to the screen// and written to the neatFile.txt // each number occupied a single line showed by fixed point recording// reserved reservedBits after the decimal point // each number occupied fieldWidth bits// either a postive or negative in front of the numberint main(){    ifstream fin;    ofstream fout;    fin.open("rawData.txt");    if (fin.fail())    {        cout << "Input file opening fail.\n";            exit(1);    }    fout.open("neatFile.txt");    if (fout.fail())    {        cout << "Output file opening fail.\n";            exit(1);    }    make_neat(fin,fout,5,10);    fin.close();    fout.close();    cout << "End the program!\n";        return 0;}void make_neat(ifstream& messyFile, ofstream& neatFile, int reservedBits, int fieldWidth){    neatFile.setf(ios::fixed);    neatFile.setf(ios::showpoint);    neatFile.setf(ios::showpos);    neatFile.precision(reservedBits);    cout.setf(ios::fixed);    cout.setf(ios::showpoint);    cout.setf(ios::showpos);    cout.precision(reservedBits);    double next;    while (messyFile >> next)    {        neatFile<< setw(fieldWidth) << next << endl;        cout << setw(fieldWidth) << next << endl;    }} 
0 0