Stdin , stdout, stderr, how can I get my program to take input from a text file and output to a text file [closed]

StackOverflow https://stackoverflow.com/questions/22130840

  •  19-10-2022
  •  | 
  •  

Question

In a programming competition , they mentioned this:

" How to answer to the problems? In the problems you are up to solve, you have to read data from a file .IN, and write the results in a file OUT. "

I am used to the regular way of programming, writing the code in IDE , compiling , then executing it to see what's going on, and it worked, however my answers were refused.

Does anyone know anything about those file.IN and .OUT thing?

I mean, how can I get my program to take input from a textfile in which I write the dat aI want to give to the program, and make it send the the output to another textfile ?

Thanks

Was it helpful?

Solution

Include stdio.h in your header files.

In your main function, include the following lines on top.

freopen("input.in","r",stdin);
freopen("output.out","w",stdout);

In most online programming competitions, input is given through stdin and output through stdout. However, in this case, you have to read input from a file (.in) and write to a file (.out). freopen takes the stream(stdin, stdout) specified as the third argument to re-open the stream and instead use the file specified.

Edit: Sample code takes input from input.in and writes to output.out.

#include <stdio.h>
int main(){
freopen("input.in","r",stdin);
freopen("output.out","w",stdout);
int n;
scanf("%d",&n);
printf("%d\n",n);
return 0;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top