流作为函数实参

下例中make_neat函数有两个流参数:一个是ifstream类型,连接到一个输入文件rawdata.txt的流。另外一个是ofstream类型的流,连接到一个输出文件neat.txt的流。

#include<iostream>
#include<fstream>
#include<cstdlib>
#include<iomanip>

using namespace std;

void make_neat(ifstream& messy_file, ofstream& neat_file, int number_after_decimalpoint, int field_width);
//前条件:messy_file和neat_file这两个流已经用open函数连接到文件。
//后条件:将与messy_file流连接的那个文件中的内容写到屏幕上。
//同时也写到与neat_file流连接的文件中。
//每个数字单独占一行,并采用定点计数法,不采用e计数法。
//在小数点后保留number_after_decimalpoint位小数。
//在每个数字之前要么加一个正号,要么加一个负号。
//每个数字都要占用fild_width的一个域宽。(该函数不关闭文件)

int main()
{
	ifstream fin;
	ofstream fout;
	
	fin.open("rawdata.txt");
	if(fin.fail())
	{
		cout << "input file opening filed.\n";
		exit(1);
	}	
	
	fout.open("neat.txt");
	if(fout.fail())
	{
		cout << "output file opening filed.\n";
		exit(1);
	}
	
	make_neat(fin, fout, 5, 12);
	
	fin.close();
	fout.close();
	
	cout << "end of program.\n";
	return 0;	
} 
 
 //使用iostream,fstream,iomanip:
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);
	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;
		neat_file << setw(field_width) << next << endl;
	}
}

 

转载于:https://my.oschina.net/u/3557041/blog/1560324

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值