In above post, we saw a way to have standard input/output from external file using file handling. In this post, we will see a very easy way to do this. Here we will compile and run our code from terminal or cmd using input and output streams.
For Windows, it’s the case of redirection operators to redirect command input and output streams from the default locations to different locations.
- Redirecting command input (<) : To redirect command input from the keyboard to a file or device, use the < operator.
- Redirecting command output (>) : To redirect command output from the Command Prompt window to a file or device, use the > operator.
Run the code
a.exe < input_file > output_file
Input from input.txt:
Compile & run:
g++ test_code.cpp a.exe < input.txt > output.txt
Output in output.txt:
I/O Redirection in linux: Input and output in the Linux environment are distributed across three streams. These streams are:
- standard input (stdin): The standard input stream typically carries data from a user to a program. Programs that expect standard input usually receive input from a device, such as a keyboard, but using < we can redirect input from the text file.
- standard output (stdout): Standard output writes the data that is generated by a program. When the standard output stream is not redirected, it will output text to the terminal. By using > we can redirect output to a text file.
- 3. standard error (stderr): Standard error writes the errors generated by a program that has failed at some point in its execution. Like standard output, the default destination for this stream is the terminal display.
Linux includes redirection commands for each stream.
Overwrite : Commands with a single bracket overwrite the destination’s existing contents.
- > – standard output
- < – standard input
- 2> – standard error
Append : Commands with a double bracket do not overwrite the destination’s existing contents.
- >> – standard output
- << – standard input
- 2>> – standard error
Here, we will use Overwrite’s command because we don’t need to append the output i.e we only want the output of one single code.
Compile C++ code
Run the code
./a.out < input_file > output_file
We can similarly give standard input/output from text files for C or Java by first compiling the code and while running the code we give input/output files in given format. For languages like python which don’t require compilation, we do the following
python test_code.py < input.txt > output.txt
Related Article: Python Input Methods for Competitive Programming
This article is contributed by Pratik Chhajer. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Top 10 Algorithms and Data Structures for Competitive Programming
- How to begin with Competitive Programming?
- How to become a master in competitive programming?
- Tips and Tricks for Competitive Programmers | Set 1 (For Beginners)
- Tips and Tricks for Competitive Programmers | Set 2 (Language to be used for Competitive Programming)
- Competitive Programming: Conquering a given problem
- Fast I/O for Competitive Programming
- A Better Way To Approach Competitive Programming
- getchar_unlocked() - faster input in C/C++ for Competitive Programming
- Bitwise Hacks for Competitive Programming
- Some important shortcuts in Competitive Programming
- Quick ways to check for Prime and find next Prime in Java
- Fast I/O in Java in Competitive Programming
- Formatted output in Java
- Frequency Measuring Techniques for Competitive Programming