×

# Codejam: File Input/Output in C++

 1 Please provide me the code snippet for taking inputs from .in files and output to .out files. asked 11 Mar '17, 15:58 228●10 accept rate: 10%

 1 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); answered 27 Mar '17, 09:47 3★shraeyas 1.3k●1●5●18 accept rate: 10%
 1 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.exea.out " or " ./prga.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 answered 11 Mar '17, 17:58 537●1●8 accept rate: 27%
 0 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 4★ayush933 97●5 accept rate: 0%
 0 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 output.txt (for generating output file )  You dont have to do anything else. Happy Coding :) answered 27 Mar '17, 07:10 2★techbuzz 52●2 accept rate: 2%
 toggle preview community wiki:
Preview

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• 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,746
×154
×123
×38

question asked: 11 Mar '17, 15:58

question was seen: 2,394 times

last updated: 27 Mar '17, 09:47