input file

How to handle the input file provided in programming contests(like facebook hacker cup)?
specifically talking about file handling in c++…

2 Likes

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.

  1. 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

2 Likes

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.

1 Like

Create a stdin file and insert the input given into it. Then while running the program , read the input from that same file.