![]() |
VOOZH | about |
In Competitive Programming, most of the time we need to enter input for checking our code manually. But it would become cumbersome if we have a questions like Graphs, strings, or bulky entry of input data because it will definitely time out or the chances of typing incorrect data would be increased.
We can easily get rid of problem by simply saving the test cases to the specified file at the desired location according to our easiness. These are useful in competitions like Facebook Hackercup, Google Codejam. These competitions do not provide the online judge like environment rather than they asked you to upload input and output files.
For Input/Output, we basically use these two type of file access modes i.e.,
C/C++
In C/C++ we can use freopen for standard input and output. The syntax of this function as:-
FILE * freopen(const char * filename, const char * mode, FILE * stream );
We can also use Symbolic constant ONLINE_JUDGE for debugging purpose. As most of the online judges add this flag at the end of the command line before executing our code. For example in C++11, it would be compiled as
-std = c++0x -O2 -static -s -lm -DONLINE_JUDGE
So we can take advantage of that by using C preprocessor directive.
we can also do the same using ONLINE_JUDGE in java, but as of now it does not work in codechef but works in codeforces.
JAVA
In Java, we can use BufferedReader class for the fast Input and PrintWriter class for formatted representation to the output along with FileReader and FileWriter class.
PYTHON
In python we first import the module sys(system), after that We will use open() function which returns the file object, that are commonly used with two arguments: open(filename, mode).