How Can You Redirect scanf to Read from a File in GCC?

  • Thread starter Thread starter Werg22
  • Start date Start date
  • Tags Tags
    Gcc
Join the discussion
Registration is free. Start your own thread to ask a follow-up.
5 replies · 2K views
Werg22
Messages
1,431
Reaction score
1
I have a code which uses scanf. How do I make the compiled code read from a (text) file instead from user's input?
 
Physics news on Phys.org
open the file with fopen() and then use fscanf()
if you search for these terms you should find lots of examples
 
That's an idea, but I need to be able to do it from the command line... I know there's a way, involving the symbol "<", I just don't remember the details.
 
If you just want the same code to read from a file you can just use:
program << file.txt
 
Thank you very much. Maybe you can answer this question too: how can I write test cases for my program, keeping in mind it has to read from files? I'm used to using "assert" for tests, by copying my program into a header file and then writing a main function with a bunch of assert statements. In this case, I can't exactly use assert. Is there a statement that opens a file and let's scanf read from it?