How to handle the input file provided in programming contests(like facebook hacker cup)?
specifically talking about file handling in c++β¦
There are several possibilities, like creating file streams, but the best one IMO is linking the files directly to stdin/stdout. Just start your program with
freopen(βinputfile.inβ,βrβ,stdin);
freopen(βoutputfile.inβ,βwβ,stdout);
Now, you can use scanf() and printf() just like you would with console I/O. And the best thing: you can switch between console and the specified files by commenting those 2 lines!
Alternative:
1)In windows : Build/ compile the file,lets say it is ββprog.cββ using your IDE e.g. Code blocks. You will get an executable called ββprogββ .save the input file(inp.txt) in the folder that has the executable file and then go to that folder from the windows command prompt and type βprog < inp.txt > opt.txtβ. Here op.txt will be the required output file.
- Ubuntu : Build/ compile the file,lets say it is prog.c using ββgcc prog.cββ. You will get an executable called β./a.outβ .save the input file(inp.txt) in the folder with executable file and then go to that folder from the Terminal and type β./a.out < inp.txt > opt.txtβ. Here op.txt will be the required output file.
Same can be done for cpp files
Open the terminal and paste the command
g++ ./a.out < inputfile.txt > outputfile.txt
Just run the command and you will get the desired output file in your folder.
Create a stdin file and insert the input given into it. Then while running the program , read the input from that same file.