Input and output Redirection in C++In this article, we will discuss input and output redirection with their examples. But before discussing input and output redirection, we must know about the redirection in C++. Redirection refers to changing the default sources or destinations of input and output streams. It alters the way a program interacts with input and output. It is a fundamental concept in operating systems and programming languages, and it is used for many purposes. It provides flexibility in handling data from different sources. Input and output operations are performed through streams representing the sequence of data or characters. The standard library provides fundamental stream objects like "cin" for input and "cout" for output. These are related to the keyboard and console, respectively. I/O redirection involves changing the default sources or destinations of input and output streams instead of interacting with the standard input and output. This redirection is achieved by associating the stream objects with file streams using the <fstream> library. Input Redirection:It is a concept that alters the source of inputs for a program or command. Instead of reading information directly from a default source like a keyboard, the input is redirected to a specific file. For example, a program designed to analyze text can read input from a file containing text data rather than prompting for input. Jtp_sample.txtExample: Let us take a C++ program to illustrate the input redirection: Output: Explanation: The above program will read words from the "jtp_sample.txt" file, count the number of words in the file, and increment the wordcount variable. After that, this program displays the total word count on the console. After that, the input file stream is closed. Output redirection:Output redirection is a concept that changes the destination of a program's output from the default output device to a specific file. It is achieved using file stream objects like "ofstream". Output redirection will support appending to existing files, making it possible to accumulate results over multiple runs. Example:Let us take a C++ program to illustrate the output redirection: Output: Explanation: The above program will check whether the file stream is successfully opened. After that, it displays an error message if the file is not present. The program generates the numbers from 1 to 10 and then writes them in the output file separated by spaces to the output files. After that, the file stream is closed. Example:Let us take an example to illustrate the Input and Output redirection in C++. Output: Explanation: This C++ program implements a simple expense tracker with options to add expenses and view a summary. It uses file streams (ofstream and ifstream) to write and read expense data to/from a file named "expenses.txt.". The program displays a menu for user interaction, allowing the addition of expenses with categories and amounts. It also allows viewing all expenses and their total, demonstrating basic file I/O operations for a practical application. Next TopicIos bad() function in C++ |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India