forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex10_33.cpp
37 lines (31 loc) · 1.07 KB
/
ex10_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
34
35
36
37
//
// ex10_33.cpp
// Exercise 10.33
//
// Created by pezy on 12/13/14.
// Copyright (c) 2014 pezy. All rights reserved.
//
// Write a program that takes the names of an input file and two output files.
// The input file should hold integers. Using an istream_iterator read the
// input file.
// Using ostream_iterators, write the odd numbers into the first output file.
// Each value should be followed by a space.Write the even numbers into the
// second file.
// Each of these values should be placed on a separate line.
//
// Run: ./a.out "../data/input.txt" "../data/odd.txt" "../data/even.txt"
#include <fstream>
#include <iterator>
#include <algorithm>
int main(int argc, char** argv)
{
if (argc != 4) return -1;
std::ifstream ifs(argv[1]);
std::ofstream ofs_odd(argv[2]), ofs_even(argv[3]);
std::istream_iterator<int> in(ifs), in_eof;
std::ostream_iterator<int> out_odd(ofs_odd, " "), out_even(ofs_even, "\n");
std::for_each(in, in_eof, [&out_odd, &out_even](const int i) {
*(i & 0x1 ? out_odd : out_even)++ = i;
});
return 0;
}