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.