Open In App

Input/Output from external file in C/C++, Java and Python for Competitive Programming | Set 2

Last Updated : 19 May, 2017
Improve
Improve
Like Article
Like
Save
Share
Report

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.

  1. Redirecting command input (<) : To redirect command input from the keyboard to a file or device, use the < operator.
  2. 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




// 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:

  1. 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.
  2. 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. 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 :


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads