流作为函数实参

#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     



                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值