You are not logged in. Please login at www.codechef.com 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, 15:58

pratik_gadhiya's gravatar image

2★pratik_gadhiya
2279
accept rate: 10%


Smply use this.

For taking inputs

freopen("C:\Users\Shraeyas\Documents\pg\pr_ag\input.in", "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("input.in", "r", stdin);

And for outputs...

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

link

answered 27 Mar, 09:47

shraeyas's gravatar image

2★shraeyas
1.1k1213
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.in >a.out " or " ./prg<a.in>a.out " for windows / linux respectively

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

for more details refer to this link for windows

link

answered 11 Mar, 17:58

diveshuttam's gravatar image

3★diveshuttam
5398
accept rate: 27%

edited 11 Mar, 18:01

link

answered 11 Mar, 16:02

bansal1232's gravatar image

4★bansal1232
2.6k110
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.

link

answered 11 Mar, 16:35

ayush933's gravatar image

3★ayush933
975
accept rate: 0%

edited 11 Mar, 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 :)

link

answered 27 Mar, 07:10

techbuzz's gravatar image

2★techbuzz
522
accept rate: 2%

toggle preview
Preview

Follow this question

By Email:

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

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "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:

×1,643
×141
×109
×38

question asked: 11 Mar, 15:58

question was seen: 1,034 times

last updated: 27 Mar, 09:47