How to Redirect cin and cout to Files in C++? Last Updated : 30 Jan, 2024 Comments Improve Suggest changes Like Article Like Report 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 C++ // C++ program to redirect cin and cout to files #include <fstream> #include <iostream> using namespace std; int main() { // Opening the input file stream and associate it with // "input.txt" ifstream fileIn("input.txt"); // Redirecting cin to read from "input.txt" cin.rdbuf(fileIn.rdbuf()); // Opening the output file stream and associate it with // "output.txt" ofstream fileOut("output.txt"); // Redirecting cout to write to "output.txt" cout.rdbuf(fileOut.rdbuf()); // Variables for input int x, y; // Reading two integers from "input.txt" cin >> x >> y; // Writing the product of the two integers to // "output.txt" cout << "Product: " << (x * y) << endl; return 0; } input.txt 10 20output.txt 200 Comment More infoAdvertise with us Next Article How to Redirect cin and cout to Files in C++? R rajveer810214 Follow Improve Article Tags : C++ Programs C++ cpp-input-output cpp-file-handling CPP Examples +1 More Practice Tags : CPP Similar Reads How to Open and Close a File in C++? In C++, we can open a file to perform read and write operations and close it when we are done. This is done with the help of fstream objects that create a stream to the file for input and output. In this article, we will learn how to open and close a file in C++. Open and Close a File in C++ The fst 2 min read How to Read and Write Arrays to/from Files in C++? In C++, an array is a collection of elements of the same type placed in contiguous memory locations. A file is a container in a computer system for storing information. In this article, we will learn how to read and write arrays to/from files in C++. Example: Input: Array = {1, 2, 3, 4, 5}Output: // 2 min read How to Read From a File in C++? Reading from a file means retrieving the data stored inside a file. C++ file handling allows us to read different files from our C programs. This data can be taken as input and stored in the program for processing. Generally, files can be classified in two types:Text File: Files that contains data i 4 min read How to Use cin.fail() Method in C++? In C++, the cin.fail() method is a part of <iostream> library that is used to check whether the previous input operation has succeeded or not by validating the user input. In this article, we will learn how to use cin.fail() method in C++. Example: Input: Enter an integer: aOutput: Invalid Inp 2 min read How to Read a File Line by Line in C++? In C++, we can read the data of the file for different purposes such as processing text-based data, configuration files, or log files. In this article, we'll learn how to read a file line by line in C++. Read a File Line by Line in C++We can use the std::getline() function to read the input line by 2 min read Like