how to redirect stdin and stdout to read data from file in c?

I want to use file as my standard i/o how do I redirect standard I/O to read data from/to file.

You can use C library function freopen() for this purpose. You can implement this like for inputting and getting corresponding output file.

freopen(“input_filename.in”,“r”,stdin) //must be open in read mode and extension should be .in
freopen(“output_filename.out”,“w”,stdout) //created in write mode and auto-generated during runtime

1 Like

You can use input redirection. If your input file is “input.txt” and your program is a.out, Run it like this:

./a < input.txt

Output redirection can also come in handy at times. Use it like this:

./a > output.txt