Skip to content

Instantly share code, notes, and snippets.

@meritozh
Last active April 8, 2016 02:01
Show Gist options
  • Save meritozh/9aefd1e9b5d649fcd4062945922217b2 to your computer and use it in GitHub Desktop.
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
#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;
}
@meritozh
Copy link
Author

meritozh commented Apr 8, 2016

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment