Instead of writing the file stream objects into your code, a simpler solution is to redirect the output just like the pipeline in Linux.
Basically, you just have to go to project properties -> Debugging -> Command Arguments, and add:
"> Output.txt".
Here is a nice post for it:
http://asawicki.info/news_1496_redirecting_output_to_file_in_visual_c.html