Codeforces celebrates 10 years! We are pleased to announce the crowdfunding-campaign. Congratulate us by the link https://codeforces.com/10years. ×

### kk_ducviet's blog

By kk_ducviet, 5 years ago, ,

Most coders read input from file and write out to another, but some online judge likes CodeForces use stdin. When solving problems in a contest, eveyone always want to minimum submission time, these little code may help:

C/C++

#ifndef ONLINE_JUDGE
freopen(fi, "r", stdin);
freopen(fo, "w", stdout);
#else
// online submission
#endif


Pascal

{$IFDEF ONLINE_JUDGE} {online submission} {$ELSE}
{\$ENDIF}


Why? Because on the online judge, ONLINE_JUDGE is defined to be true in C/C++

Notice that it also works on UVa, some others too but I didn't test it :)

• +6

 » 5 years ago, # |   +4 It's better to define your own preprocessor option, for example LOCAL_PROJECT (and use it in all online judges).You can do it using -DLOCAL_PROJECT for g++. And just check it with #ifdef LOCAL_PROJECT freopen("a.in","r",stdin); //freopen("a.out","w",stdout); // console output is better (in most cases) #else // add i/o method of specific testing system #endif MS VS users: Project -> Options -> C\C++ -> Preprocessor options. And to use console output for debugging: Use Ctrl+F5 or _getch(); under #ifdef #endif in the end of your program. In this case, #include  must me under #ifdef #endif too. Don't forget to run your project in Release configuration sometimes, it's much faster than Debug, but debugger won't work properly.
•  » » 5 years ago, # ^ |   0 Thanks, I think this better than mine
•  » » 3 weeks ago, # ^ | ← Rev. 2 →   -23 then in this case we can't use #include #include bits/stdc++.h ?? right?
 » 5 years ago, # |   +2 i usually read input from a file, but don't write output to a file (i use normal stdout). during contests, all i do is change the command ./a.out to ./a.out < input.txt (works only on Linux-based OS, but similar addition can be made for other OS too).
•  » » 5 years ago, # ^ |   -8 boring!
•  » » 5 years ago, # ^ |   +5 Don't lie to us, bash-like redirection also works in Windows, and, obviously, Mac, because it uses bash :)
 » 5 years ago, # |   -10 It's pretty useless, because fast in/out only needed in 0.001% of cases.
•  » » 5 years ago, # ^ |   0 I join contest with friends and once I submited it faster than all of them.
•  » » 5 years ago, # ^ |   0 There are a lot of problems where largest input is like 2 MB or even more.
•  » » 3 years ago, # ^ |   0 It's not about how many ms this saves you. It eases your life alot when implementing. You don't have to manually input in the terminal. It gets you the input from the file, thus you see the answer in output file or stdout in no time!
 » 4 years ago, # |   +4 in Java: if (System.getProperty("ONLINE_JUDGE") == null) { // Input is a file } else { // Input is System.in }