This article is about easiest way to perform read and write operation from file. File handling is one of the most important aspect of programming and it becomes more important when we do competitive programming. Here are some context when you need to deal with files in comptetive programming.
1) Some specific programming contests like Google Code Jam and Facebook Hacker Cup they provide a file for input. You have to read input from that file and write answers into a file and upload on server within limited time.
2) If you are trying to code for a problem. Suppose the problem was that a square matrix is given and you have to rotate the matrix by 90 degree clock-wise. First line of input indicate the total no. of test cases and first line before matrix represents size of matrix. Then input would be like this.
3 5 10 23 43 34 21 11 13 43 30 71 10 23 43 24 21 13 53 43 34 21 10 23 43 38 61 3 2 4 5 6 4 1 2 8 9 4 31 20 40 21 77 22 61 10 11 22 33 44 43 88 41 90If you take the input from console(keyboard) it requires 149 strokes. You will have to hit 149 strokes everytime you test you program.
File handling is a ornery job and you never want to deal with rather than conecentrating on cracking the problem in the live competition. So I am writting about a method which is not only quite easy but you can toggle from file to console by just commenting and uncommenting a single line. We are talking about diverting the stream of input and output. Almost all major programming evnvironment by default connect I/O stream to console, but you can divert it into file I/O stream by using the follwing code snippets, respectively for C/C++ and java.
C/C++
#include<cstdio> #include<iostream> #include<cstring> using namespace std; void program(); int main() { //Diverting the input stream into file "temp.in".Comment the below line to use console freopen("temp.in", "r", stdin); //Diverting the output stream into file "temp.out".Comment the below line to use console freopen("temp.out", "w", stdout); int t=1; scanf("%d",&t);//Comment this line if there is only one test case while(t--) { program(); } return 0; } void program() { char s[20]; scanf("%s",s); printf("The input string was %s\n",s); }
Java
import java.util.Scanner; import java.io.*; public class Template { public static void main(String args[]) { try { FileOutputStream output=new FileOutputStream("temp.out"); PrintStream out=new PrintStream(output); //Diverting the output stream into file "temp.out".Comment the below line to use console System.setOut(out); InputStream input=new FileInputStream("temp.in"); //Diverting the input stream into file "temp.in".Comment the below line to use console System.setIn(input); } catch (FileNotFoundException e) { e.printStackTrace(); } Scanner sc=new Scanner(System.in); int t=1; //t=sc.nextInt();//Comment this line if there is single test case while((t--) != 0) { program(sc); } } }
Note:- Keep the files ("temp.in" and "temp.out") in the same directory of source code. See the comments in the above code and don't get surprised for .in and .out extension, It's nothing just differetiate the Input and Output files and can be treated as plain text files. For any further clarification please comment.
Java solution for above mentioned problem
import java.util.Scanner; import java.io.*; public class Solution { public static void main(String args[]) { try { FileOutputStream output=new FileOutputStream("test.out"); PrintStream out=new PrintStream(output); //System.setOut(out); InputStream input=new FileInputStream("test.in"); System.setIn(input); } catch (FileNotFoundException e) { e.printStackTrace(); } Scanner sc=new Scanner(System.in); int t=1; t=sc.nextInt();//Comment this line if there is single test case while((t--) != 0) { program(sc); } } public static void program(Scanner sc) { int[][] table=new int[20][20]; int N; N=sc.nextInt(); for(int i=0;i<N;++i) { for(int j=0;j<N;++j) { table[i][j]=sc.nextInt(); } } System.out.println("The content of matrix are"); for(int i=0;i<N;++i) { for(int j=0;<N;++j) { System.out.print(table[i][j]+" "); } System.out.println(); } System.out.println("After rotating 90 degree"); rotate(table,N); } public static void rotate(int[][] matrix,int n) { for(int layer=0;layer < (n/2); ++layer) { int first=layer; int last=n-1-layer; for(int i=first;i<last;++i) { int offset=i-first; // save top int top = matrix[first][i]; // left -> top matrix[first][i] = matrix[last-offset][first]; // bottom -> left matrix[last-offset][first] = matrix[last][last - offset]; // right -> bottom matrix[last][last - offset] = matrix[i][last]; // top -> right matrix[i][last] = top; } } for(int i=0;i<n;++i) { for(int j=0;j<n;++j) { System.out.print(matrix[i][j]+" "); } System.out.println(); } } }
Some competitive programming problems involve a lot of input and/or a lot of output, which makes basic I/O too slow in Java. I have had better luck using BufferedReader and BufferedWriter. Here's a post I wrote on that topic, with some benchmarks: Why is Java I/O Slow?.
ReplyDeleteYes, I agree Scanner method is fairly slow in java but I am suggesting here the redirection of stream.
DeleteOnce you redirect your default(console) stream to file stream you can use any of the faster method like BufferedReader and BufferedWriter. For taking input you will do something like
BufferedReader input = new BufferedReader (new InputStreamReader(System.in));
In above line we passed the "System.in" and we already redirected it to a file.
What about using Redirection(<<,>>)?
ReplyDeleteThey'll fill the purpose very easily.. :)
Eg. [me@linuxbox me]$ sort < file_list.txt > sorted_file_list.txt
Yes. It's easier to use Redirection(<<,>>) but you can do it via terminal. I wanted a way to use in code itself.
Delete