iostream迭代器的使用(11.18)

#include<iostream>
#include<fstream>
#include<string>
#include<iterator>
#include<cstdlib>
using namespace std;

ofstream & openFile(ofstream &out,const string &fileName)
{
	out.close();
	out.clear();
	out.open(fileName.c_str());
	return out;
}

ifstream & openFile(ifstream &in,const string &fileName)
{
	in.close();
	in.clear();
	in.open(fileName.c_str());
	return in;
}

int main()
{
	cout<<"Enter two file name:"<<endl;
	string filename1,filename2;
	cin>>filename1>>filename2;

	ofstream outfile1,outfile2;
	openFile(outfile1,filename1);
	openFile(outfile2,filename2);

	cout<<"Enter some integers:"<<endl;
	istream_iterator<int> input(cin),eof;
	ostream_iterator<int> output1(outfile1," ");
	ostream_iterator<int> output2(outfile2,"\n");
	
	while(input!=eof)
	{
		if(*input%2)
			*output1++=*input++;
		else
			*output2++=*input++;
	}
	outfile1.close();
	outfile2.close(); //close file


	cout<<"odd number contains:"<<endl;
	ifstream infile;
	openFile(infile,filename1);
	istream_iterator<int> in(infile),end_of_stream;
	ostream_iterator<int> out(cout," ");
	while(in!=end_of_stream)
		*out++=*in++;
	cout<<endl;

	return 0;
}

你可能感兴趣的:(iostream迭代器)