VOOZH about

URL: https://www.geeksforgeeks.org/cpp/redirect-cin-and-cout-to-files-in-cpp/

⇱ How to Redirect cin and cout to Files in C++? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to Redirect cin and cout to Files in C++?

Last Updated : 23 Jul, 2025

In C++, we often ask for user input and read that input using the cin command and for displaying output on the screen we use the cout command. But we can also redirect the input and output of the cin and cout to the file stream of our choice.

In this article, we will look at how to redirect the cin and cout to files in C++.

Redirecting cin and cout to a File in C++

For redirecting cin and cout to a file, we first create the input file stream and output file stream to our desired file. Then using rdbuf() function of stream objects, we redirect the input for cin to the file and output for cout to the file.

C++ Program to Redirect cin and cout to a File


input.txt

10 20

output.txt

200
Comment