Last active
April 8, 2016 02:01
-
-
Save meritozh/9aefd1e9b5d649fcd4062945922217b2 to your computer and use it in GitHub Desktop.
read numbers from a file stream, split them to evens and odds then print to other two file streams. C++ Primer 5th 10.33
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <fstream> | |
int main(int argc, char const *argv[]) | |
{ | |
std::ifstream input(argv[1]); | |
std::ofstream output_odd_number(argv[2]); | |
std::ofstream output_even_number(argv[3]); | |
std::istream_iterator<int> in_iter(input), eof; | |
std::ostream_iterator<int> output_odd_number_iter(output_odd_number, " "); | |
std::ostream_iterator<int> output_even_number_iter(output_even_number, " "); | |
while (in_iter != eof) | |
((*in_iter % 2 == 0) ? output_even_number_iter : output_odd_number_iter) = *in_iter++; | |
output_even_number << std::endl; | |
output_odd_number << std::endl; | |
output_odd_number.close(); | |
output_even_number.close(); | |
input.close(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
for zsh user, if no line 16 and 17,
cat
output files will print a percent sign. Otherwise,export PROMPT_EOL_MARK=""
to solve it.more information: percent sign