Freopen();

Recently i came across some submissions using these two things

freopen(“input.txt”, “r”, stdin);
freopen(“output.txt”, “w”, stdout);

What is the main purpose of using this? …

it is used when you have input in a file name “input.txt” and want to put the output in the file “output.txt”
when you use these operation then when you use cin then instead of taking the console input it takes it from the file input.txt and when you output it puts the result in the file “output.txt”

Why are files used in online programming?

It is easier because you don’t have to type the input out again and again

Those statements are probably in a conditionally compiled part of the source, something like

  #ifndef USE_ONLINE_JUDGE 
   freopen("input.txt", "r", stdin);
   freopen("output.txt", "w", stdout);
 #endif

On Codechef USE_ONLINE_JUDGE is defined during compilation, so those statements are not compiled. When testing on your local machine it is not defined. So instead of having to type input it is read from a file and output is saved to another file

1 Like
//