이 기사에서는 C/C++, Java 및 Python for Competitive Programming에서 외부 파일의 입력/출력에 대해 알아봅니다.
파일의 Python I/O
파이썬에서 sys 모듈은 파일에서 입력을 받아 파일에 출력을 쓰는 데 사용됩니다. 코드 형태의 구현을 살펴보자.
예시
import sys
# For getting input
sys.stdin = open('sample.txt', 'r')
# Printing the Output
sys.stdout = open('sample.txt', 'w') 파일의 Java I/O
여기에서 버퍼링된 판독기 메서드를 사용하여 파일 판독기와 연결된 입력을 가져와 파일에서 입력을 읽고 인쇄기에서 데이터를 다시 파일로 인쇄합니다.
예시
// 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
여기에서 free open() 함수의 도움을 받아 파일을 열 모드와 수행하려는 작업의 종류를 정의합니다. 기본 모드는 읽기 전용으로 설정되어 있습니다.
예시
#include<stdio.h>
int main() {
// For getting input
freopen("sampleinp.txt", stdin);
// Printing the Output
freopen("sampleout.txt", "w", stdout);
return 0;
} 결론
이 자습서에서는 C/C++, Java 및 Python for Competitive Programming의 외부 파일에서 입력/출력에 대해 알아봅니다.