szawinis's blog

By szawinis, history, 8 years ago, In English

So, just a few hours ago, we finished a contest on a Thai site, namely Codecube. The round consisted of 6 problems to solve in 3 hours. I was feeling confident at first ACing two problems in 15 minutes. Then came the third problem, which was a typical two pointers + data structures. I was almost certain I could solve the problem, and started to code the solution. Many minutes passed by, and I finally got the solution. I submitted it, and got a WA slapped on my face. So, I simply moved on to the fourth problem and ACed it, because finding bugs is time consuming. I later returned to the third problem, and tried finding the bug for almost an hour with no luck, only to find out that the only line I messed up on was: freopen("data.txt", "r", stdin);, which I forgot to uncomment. I later submitted after the contest and got AC just with that line commented out. Absolutely disappointing.

As many of us know, the freopen command is used to redirect stdio to file IO. This is particularly useful when testing your program on a local machine because you can avoid copying and pasting the test data every time you want to debug. However, one of the problems I have experienced when using it is I forget to uncomment it out when submitting. This is a huge problem, especially on CF where many points are deducted for resubmission. I don't know if it's bad luck, but this has happened to me several times already. For anyone who is experiencing the same problem, I think the solution here is simply to write comments to remind yourself. Has this ever happened to any of you guys? Or is it just me being awfully stupid?

  • Vote: I like it
  • -8
  • Vote: I do not like it

»
8 years ago, # |
  Vote: I like it +14 Vote: I do not like it

There is a way to avoid commenting and uncommenting that line. Simply use:

#ifndef ONLINE_JUDGE
freopen("data.txt","r",stdin);
#endif

Personally, I don't use it, as my compiler allows me to redirect a file to stdin automatically. However, it should help you.

»
8 years ago, # |
Rev. 2   Vote: I like it +24 Vote: I do not like it

I learn this from somewhere:

if (fopen("data.txt", "r")) {
   freopen("data.txt", "r", stdin);
}

It works in onsite contest as well.

»
8 years ago, # |
  Vote: I like it +7 Vote: I do not like it

./executable < input.txt

I worried about not commenting fread(), so I switched to this.
Type once, use command history to use as many times as you want.

»
8 years ago, # |
  Vote: I like it +4 Vote: I do not like it

My old ICPC teammate tiagob.reis used to do this all the time when we were training (redirect input and output and send the code to the judge without changing that), so almost all the problems he submitted had at least a +1 penalty — the verdicts varied from WA, TLE and RE (I can't remember, but in one judge we might have gotten some kind of forbidden operation error).

Fortunately, this never happened on any onsite contest for us.