Nathan4's blog

By Nathan4, history, 3 months ago, In English
Why am I writing this

Here's two pretty useful command lines which run your code on local .txt files and write the results as .txt files (This is the input and output format required by the upcoming Hacker Cup!):

After you have your solution ready, first run the following in your terminal (I'm using vscode), be sure that you are in the same directory as your code:

g++ -o [output executable file name] [code file]

For example, when I am solving problem A, I will run:

g++ -o a a.cpp

After this, your terminal should return nothing and work fine. This first line compiles the code for you. Next, you can directly run the Executable with Input/Output redirection:

./[name of your executable file name] < [name of input txt file] > [name of output txt file]

So for the previous example, I will then run:

./a < ain.txt > aout.txt

Be sure that the input txt file's directory is also correct, and you should see aout.txt appearing in the same folder as your code file, and the results lie inside.

Hope that no one will be stuck in this step again for the first round, and wish all of u good luck (I didn't notice any written tutorial about this, but feel free to link other good ones :D)

  • Vote: I like it
  • +22
  • Vote: I do not like it

»
3 months ago, # |
  Vote: I like it 0 Vote: I do not like it

I was changing line by line of cin to inputFile last round, which is defined by ifstream.. This is working for me, thanks

»
3 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Another Method

create a file named output.txt in your code directory and add these lines in your main function

#ifndef ONLINE_JUDGE
freopen("output.txt", "w", stdout);
#endif
  • »
    »
    3 months ago, # ^ |
      Vote: I like it +13 Vote: I do not like it

    there's no need for the ifndef