You are not logged in. Please login at to post your questions!


Codejam: File Input/Output in C++

Please provide me the code snippet for taking inputs from .in files and output to .out files.

asked 11 Mar '17, 15:58

pratik_gadhiya's gravatar image

accept rate: 10%

Smply use this.

For taking inputs

freopen("C:\Users\Shraeyas\Documents\pg\pr_ag\", "r", stdin);

And for outputs...

freopen("C:\Users\Shraeyas\Documents\pg\pr_ag\output.out", "w", stdout);

Since I keep both the input output files and source code in a different Disk drive so I have to write the whole path to access the input files. If you keep all the files in the same folder then you can simply use this...

For taking inputs

freopen("", "r", stdin);

And for outputs...

freopen("output.out", "w", stdout);


answered 27 Mar '17, 09:47

shraeyas's gravatar image

accept rate: 10%

Adding to the above answers yet another way is to use I/O redirection. Which is better in a sense that you don't need to change code for testing on console or running on file.Also it is language independent. Here it goes,

  • Code using cin/cout or scanf/printf as you wish
  • Build the program (Say the final executable is prg.exe (on windows) or binary is ./prg (on linux))
  • Open Command prompt/ terminal , change to the directory(folder) where the program is using cd "path of directory"
  • run" prg.exe< >a.out " or " ./prg<>a.out " for windows / linux respectively

Make sure that (or whatever the file is) is in the same directory(folder)

for more details refer to this link for windows


answered 11 Mar '17, 17:58

diveshuttam's gravatar image

accept rate: 27%

edited 11 Mar '17, 18:01


answered 11 Mar '17, 16:02

bansal1232's gravatar image

accept rate: 16%

See this. After this just replace cin with "fin" and cout with "fout"(without quotes) everywhere you use them. You need not create an output file. It will automatically be created when you run the code in an IDE.


answered 11 Mar '17, 16:35

ayush933's gravatar image

accept rate: 0%

edited 11 Mar '17, 16:36

For contests like Facebook Hacker Cup , Google Codejam , APAC you have to download an input file, generate an output file and then upload it within a given timer. So the best way to do it is : Use Linux environment for this, Suppose input file name is input.txt. Your source file is Q1.cpp Write this in command terminal -

g++ Q1.cpp ( for compiling)

./a.out <input.txt> output.txt (for generating output file )

You dont have to do anything else. Happy Coding :)


answered 27 Mar '17, 07:10

techbuzz's gravatar image

accept rate: 2%

toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:


question asked: 11 Mar '17, 15:58

question was seen: 2,394 times

last updated: 27 Mar '17, 09:47