forked from jaege/Cpp-Primer-5th-Exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
10.33.cpp
33 lines (30 loc) · 933 Bytes
/
10.33.cpp
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
#include <fstream>
#include <iostream>
#include <iterator>
#include <algorithm>
int main(int argc, char**argv) {
if (argc < 4) {
std::cerr << "Usage: 10.33 <input filename> <odd number output filename> "
"<even number output filename>" << std::endl;
return -1;
}
std::ifstream in(argv[1]);
if (!in.is_open()) {
std::cerr << "Cannot open file: " << argv[1] << std::endl;
return -2;
}
std::ofstream out_odd(argv[2]), out_even(argv[3]);
if (!out_odd.is_open()) {
std::cerr << "Cannot open file: " << argv[2] << std::endl;
return -2;
}
if (!out_even.is_open()) {
std::cerr << "Cannot open file: " << argv[3] << std::endl;
return -2;
}
std::istream_iterator<int> i_iter(in), eof;
std::ostream_iterator<int> odd_iter(out_odd, " "), even_iter(out_even, "\n");
std::for_each(i_iter, eof,
[&](int i) { i % 2 ? odd_iter = i : even_iter = i; });
return 0;
}