1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| /************************************************************************* > File Name: test.10.33.cpp > Author: puruidong > Mail: 1@w1520.com > Created Time: 2014年06月22日 ************************************************************************/ #include<iostream> #include<fstream> #include<vector> #include<iterator> #include<algorithm> using namespace std; /********************************* 编写程序,接受三个参数:一个输入文件和两个输出文件的文件名. 输入文件保存的应该是整数.使用istream_iterator读取输入文件.使用 ostream_iterator将奇数写入第一个输出文件,每个值之后都跟一个空格.将偶数 写入第二个输出文件,每个值都独占一行. *****************************************/ void outs(string file,bool wr) { fstream f(file); istream_iterator<string> ins(f); istream_iterator<string> eof; vector<string> vecs(ins,eof); if(wr) { ostream_iterator<string> outins(cout," "); copy(vecs.begin(),vecs.end(),outins); }else { ostream_iterator<string> outins(cout,"\n"); copy(vecs.begin(),vecs.end(),outins); } } void rwfile(string readfile,string writefile1,string writefile2) { fstream f(readfile); fstream fw1(writefile1); fstream fw2(writefile2); istream_iterator<int> ins(f); istream_iterator<int> eof; vector<int> vec(ins,eof); //cout << vec.size() << endl; // unique_copy(ins,eof,vec); ostream_iterator<int> out1(fw1," "); ostream_iterator<int> out2(fw2,"\n"); fw1 << "**********************\n奇数\n*******************" << endl; fw2 << "*********************\n偶数\n********************" << endl; for(vector<int>::iterator it=vec.begin();it!=vec.end();++it) { if(*it%2==0) { out2=*it; }else { out1 = *it; } } fw1 << "\n*****************奇数******" << endl; fw2 << "\n*******************偶数********" << endl; bool wr = true; outs(writefile1,wr); cout << "\n\n*************此输出为程序再次读取文件[已写入完成]的输出,对格式进行了处理,但是未改变源文件格式,具体可看源文件.******************\n\n" << endl; outs(writefile2,wr=false); } int main() { //rwfile rwfile("test.10.33.readfile.txt","test.10.33.rout1.txt","test.10.33.rout2.txt"); return 0; } |