Skip to main content

Easiest Method to Read and Write into Files Streams in Competitive Programming

                     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 90
If 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();
 }
}
}


Comments

  1. 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?.

    ReplyDelete
    Replies
    1. Yes, I agree Scanner method is fairly slow in java but I am suggesting here the redirection of stream.
      Once 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.

      Delete
  2. What about using Redirection(<<,>>)?
    They'll fill the purpose very easily.. :)
    Eg. [me@linuxbox me]$ sort < file_list.txt > sorted_file_list.txt

    ReplyDelete
    Replies
    1. Yes. It's easier to use Redirection(<<,>>) but you can do it via terminal. I wanted a way to use in code itself.

      Delete

Post a Comment

Popular posts from this blog

com.mongodb.MongoCommandException: Command failed with error 18: 'Authentication failed.' on server

If you are trying to connect Mongo DB Server and it insanely throwing following error. com.mongodb.MongoTimeoutException : Timed out after 1000 ms while waiting for a server that matches ReadPreferenceServerSelector{readPreference=primary}. Client view of cluster state is {type=UNKNOWN, servers=[{address=192.168.1.10:27010, type=UNKNOWN, state=CONNECTING, exception={ com.mongodb.MongoSecurityException: Exception authenticating MongoCredential {mechanism=null, userName='user123', source='admin', password=<hidden>, mechanismProperties={}}}, caused by {com.mongodb.MongoCommandException: Command failed with error 18 : 'Authentication failed.' on server 192.168.1.10:27010 . The full response is { "ok" : 0.0, "code" : 18, "errmsg" : "Authentication failed." }}}] If you start looking the error content First you encounter with Timeout Exception which may mislead you. It is basically an authentication error. I...

How to Create a Namespace in Aerospike Database

                      T his post is about creating a namespace in Aerospike. I could not find any concrete method to create a namespace like create database in MySQL and MongoDB. So I am suggesting a way to create a namespace in Aerospike Database. Step-1: Locate config file aerospike.conf and open it in your favorite editor and make sure you have permission to modify the file. In my system the path of file /etc/aerospike/aerospike.conf (Default in Ubuntu). Here the content of the file. # Aerospike database configuration file. service { user root group root paxos-single-replica-limit 1 # Number of nodes where the replica pidfile /var/run/aerospike/asd.pid service-threads 4 transaction-queues 4 transaction-threads-per-queue 4 proto-fd-max 15000 } logging { # Log file must be an absolute path. file /var/log/aerospike/aerospike.log { context any info } } net...

java.lang.IllegalArgumentException: Could not instantiate implementation: org.janusgraph.diskstorage.cassandra.thrift.CassandraThriftStoreManager

If you are trying to get started with Janus Graph with Apache Cassandra. You may get the following error. Caused by: org.janusgraph.diskstorage.TemporaryBackendException: Temporary failure in storage backend at org.janusgraph.diskstorage.cassandra.thrift.CassandraThriftStoreManager.getCassandraPartitioner(CassandraThriftStoreManager.java:219) ~[janusgraph-cassandra-0.2.0.jar:na] at org.janusgraph.diskstorage.cassandra.thrift.CassandraThriftStoreManager.<init>(CassandraThriftStoreManager.java:198) ~[janusgraph-cassandra-0.2.0.jar:na] ... 48 common frames omitted Caused by: org.apache.thrift.transport.TTransportException: java.net.ConnectException: Connection refused (Connection refused) at org.apache.thrift.transport.TSocket.open(TSocket.java:187) ~[libthrift-0.9.2.jar:0.9.2] at org.apache.thrift.transport.TFramedTransport.open(TFramedTransport.java:81) ~[libthrift-0.9.2.jar:0.9.2] at org.janusgraph.diskstorage.cassandra.thrift.thriftpool.CTConnectionFactory.makeR...