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

In competitive programming, reading input from files and writing output to files is essential for testing solutions locally. This article covers file I/O techniques in Python, Java, and C/C++.

Python I/O from File

Python uses the sys module to redirect standard input and output to files. This approach allows you to use regular input() and print() functions while reading from and writing to files ?

Example

import sys

# Redirect input from file
sys.stdin = open('input.txt', 'r')

# Redirect output to file  
sys.stdout = open('output.txt', 'w')

# Now use regular input() and print()
name = input().strip()
age = int(input())

print(f"Name: {name}")
print(f"Age: {age}")

# Close files (optional - happens automatically at program end)
sys.stdin.close()
sys.stdout.close()

Java I/O from File

Java uses BufferedReader with FileReader for input and PrintWriter with FileWriter for output. This combination provides efficient file operations ?

Example

import java.io.*;

class FileIO {
    public static void main(String[] args) throws IOException {
        // Input from file
        BufferedReader br = new BufferedReader(new FileReader("input.txt"));
        
        // Output to file
        PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")));
        
        // Read and process data
        String line = br.readLine();
        int number = Integer.parseInt(br.readLine());
        
        // Write output
        pw.println("Input line: " + line);
        pw.println("Number: " + number);
        
        // Close streams
        br.close();
        pw.close();
    }
}

C/C++ I/O from File

C/C++ uses freopen() function to redirect standard input/output streams to files. This allows using scanf(), printf(), cin, and cout with files ?

C Example

#include <stdio.h>

int main() {
    // Redirect input from file
    freopen("input.txt", "r", stdin);
    
    // Redirect output to file
    freopen("output.txt", "w", stdout);
    
    char name[100];
    int age;
    
    // Read using standard functions
    scanf("%s %d", name, &age);
    
    // Write using standard functions
    printf("Name: %s\n", name);
    printf("Age: %d\n", age);
    
    return 0;
}

C++ Example

#include <iostream>
#include <cstdio>
using namespace std;

int main() {
    // Redirect input and output
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    
    string name;
    int age;
    
    // Use cin and cout normally
    cin >> name >> age;
    cout << "Name: " << name << endl;
    cout << "Age: " << age << endl;
    
    return 0;
}

Comparison

Language Input Method Output Method Complexity
Python sys.stdin redirection sys.stdout redirection Simple
Java BufferedReader + FileReader PrintWriter + FileWriter Moderate
C/C++ freopen() stdin freopen() stdout Simple

Best Practices

File Naming: Use consistent names like input.txt and output.txt for easy testing.

Error Handling: Always handle file not found exceptions in production code.

Resource Management: Close file streams when finished, though most languages handle this automatically at program termination.

Conclusion

File I/O redirection allows competitive programmers to test solutions efficiently using sample inputs. Python and C/C++ offer simple redirection methods, while Java requires explicit stream handling but provides more control over the process.

Updated on: 2026-03-25T06:18:13+05:30

506 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements