
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Input Output from External File in C, C++, Java, and Python for Competitive Programming
In this article, we will learn about Input/Output from an external files in C/C++, Java, and Python for Competitive Programming.
Python I/O from the file
In python, the sys module is used to take input from a file and write output to the file. Let’s look at the implementation in the form of code.
Example
import sys # For getting input sys.stdin = open('sample.txt', 'r') # Printing the Output sys.stdout = open('sample.txt', 'w')
Java I/O from the file
Here we take the help of buffered reader method to take input associated with file reader to read input from file and print writer to print the data back to the file.
Example
// Java program For handling Input/Output import java.io.*; class Input { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new FileReader("sampleinp.txt")); PrintWriter pw=new PrintWriter(new BufferedWriter(new FileWriter("sampleout.txt"))); pw.flush(); } }
C/C++ I/O from a file
Here we take the help of free open() function and define the mode in which we want to open the file and what kind of operation we want to perform. By default mode is set to read-only
Example
#include<stdio.h> int main() { // For getting input freopen("sampleinp.txt", stdin); // Printing the Output freopen("sampleout.txt", "w", stdout); return 0; }
Conclusion
In this tutorial, we will learn about Input/Output from an external files in C/C++, Java, and Python for Competitive Programming.
Advertisements