How to divert file input to standard input?

I've been working on c++ for some time and I know the basics. I know how to input from files with FILE and ifstream but my friend told me that there is a way to divert file input to standard input so I can read from a file with scanf. How do I do it?
Last edited on
As above, you can open the file and use use fscanf where you previously used scanf.

Or you can use redirection as suggested. Redirection happens outside of your program, a program copies the file to stdout, and the shell redirects that stdout to the stdin in your program. The input appears as if you had typed it in.

Here's a Windows example. Select Start->Run and enter cmd.exe. This should give you a console.
Use notepad to create a file, data.txt, and enter some text:
notepad data.txt
You can see the content with type. type reads each line and writes it to stdout, which you see on the console.
type data.txt
To redirect that into your program, use a pipe. I'll assume your program is called myprog.exe and is in the currect directory.
type data.txt | myprog
Last edited on
That wasn't what I was looking for but never mind I found out that what I was looking for is
freopen()
Topic archived. No new replies allowed.