#ifndef ONLINE_JUDGE ?

GOOGLED IT YET COULDNT FIND ANTHING


inline void debugMode() {

#ifndef ONLINE_JUDGE

freopen("input.txt", "r", stdin);

freopen("output.txt", "w", stdout);

#endif // ONLINE_JUDGE

}


i was solving a problem on codeforces, though i couldn’t do so… so i tried looking at someone else’s code and found this. As being a newbie couldn’t get why are we looking for if ONLINE_JUGES CONSTANT is set, if not opening a file in read mode… are we taking all inputs in one go to fasten process?? can anyone be a bit descriptive to this as i have already looked at codechef and stackoverflow and codeforces links explaining this…

2 Likes

This is a general practice by c++ programmers who code in a text editor rather than an IDE. By default the ONLINE_JUDGE constant is defined when submitting code in most online judges like Codechef and Codeforces. Thus it provides a way for the code to determine whether the code is being run in an online judge or a local machine, where the constant is not defined in the latter. Generally this flag is used in order for the code to read & write from a file rather than stdin when run on a local machine and read & write from stdin and stdout respectively when run in an online judge. Thus it eliminates the need to edit the code, for reading from a file, and again for reading from stdin.

Refer to this video for more details : https://m.youtube.com/watch?v=DlPTG4AkuqU

13 Likes

thanks… finally got it :slight_smile:

1 Like

My pleasure

1 Like

One can watch the above video as well.It is good
2 Likes

Thanks a lot! This link was of great help!

Is it helpful in “interactive problems”?

No, you can’t use it for an interactive problem. For interactive problems we’ve to use the inbuilt terminal.

1 Like

I didn’t get this sentence…i mean here we are pointing stdin to read from input.txt,that means we need to edit the code(commenting the online_judge) for reading from console right?