#include<iostream>
#include<fstream>
#include<cstdlib>
#include<iomanip> //使用setw必须
using namespace std;
void make_neat(ifstream& messy_file,ofstream& neat_file,
int number_after_decimalpoint,int field_width);
//每个数字占用的宽度field_width
int main()
{
ifstream fin;
ofstream fout;
fin.open("rawdata.txt");
if(fin.fail())
{cout<<"input file opening failed \n";
exit(1);}
fout.open("neat.txt");
if(fout.fail())
{cout<<"output file opening failed \n";
exit(1);}
make_neat(fin,fout,5,12);
fin.close();
fout.close();
cout<<"end of program \n";
return 0;
}
void make_neat(ifstream& messy_file,ofstream& neat_file, //流参数必须是引用调用
int number_after_decimalpoint,int field_width)
{
neat_file.setf(ios::fixed);
neat_file.setf(ios::showpoint);
neat_file.setf(ios::showpos);
neat_file.precision(number_after_decimalpoint); //precision 输出流的成员函数保留小数
cout.setf(ios::fixed); //在正号前面显示正号
cout.setf(ios::showpoint);
cout.setf(ios::showpos);
cout.precision(number_after_decimalpoint);
double next;
while(messy_file>>next) //如果还有要去读的数字就满足条件
{
cout<<setw(field_width)<<next<<endl; //setw 操作元 指定不同的域宽
neat_file<<setw(field_width)<<next<<endl;
}
}
out_stream.setf(ios::fixed);
out_stream.setf(ios::pointer);
out_stream.precision(2);
/*可以为任何输出流使用这些格式化命令,precision(2)保证保留2位小数点。setf 是set flag标志寄存器 .ios::fixed 标志导致流采用所谓的定点表示法。一旦设置ios::fixed 那么输出到那个流的所用浮点数会采用日常生活中常规方法写入而不是e计数法。ios::showpoint 标志要求输出流总是在浮点数(double)包含一个小数点。*/
ft