Input/Output from external file in C/C++, Java and Python for Competitive Programming | Set 2
Last Updated :
19 May, 2017
Prerequisites : Input/Output from external file in C/C++, Java and Python for Competitive Programming
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.
Windows Environment
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
CPP
// A simple C++ code (test_code.cpp) which takes
// one string as input and prints the same string
// to output
#include <iostream>
using namespace std;
int main()
{
string S;
cin >> S;
cout << S;
return 0;
}
Input from input.txt:
GeeksForGeeks
Compile & run:
g++ test_code.cpp
a.exe < input.txt > output.txt
Output in output.txt:
GeeksForGeeks
Linux Environment
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
g++ file_name.cpp
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
References :
Similar Reads
Input/Output from external file in C/C++, Java and Python for Competitive Programming In Competitive Programming, most of the time we need to enter input for checking our code manually. But it would become cumbersome if we have a questions like Graphs, strings, or bulky entry of input data because it will definitely time out or the chances of typing incorrect data would be increased.
4 min read
Fast I/O for Competitive Programming in Python In Competitive Programming, it is important to read input as fast as possible to save valuable time. Input/Output in Python can be sometimes time taking in cases when the input is huge or to output any numbers of lines or a huge number of arrays(lists) line after line.Fast InputNormally, the input i
3 min read
Python Input Methods for Competitive Programming Python is an amazingly user-friendly language with the only flaw of being slow. In comparison to C, C++, and Java, it is quite slower. In online coding platforms, if the C/C++ limit provided is x. Usually, in Java time provided is 2x, and in Python, it's 5x. To improve the speed of code execution fo
6 min read
Tips and Tricks for Competitive Programmers | Set 2 (Language to be used for Competitive Programming) This is a question asked quite often: In which language should be preferred to be efficient in competitive programming? It is something one should not worry about as what matters is the logic, not the language. Most of the languages are more or less the same, but till now, the most preferred languag
5 min read
Dividing a Large file into Separate Modules in C/C++, Java and Python If you ever wanted to write a large program or software, the most common rookie mistake is to jump in directly and try to write all the necessary code into a single program and later try to debug or extend later. This kind of approach is doomed to fail and would usually require re-writing from scrat
14 min read