Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Very simple question about gcc (C compilation)

  1. Oct 25, 2008 #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?
     
  2. jcsd
  3. Oct 25, 2008 #2

    mgb_phys

    User Avatar
    Science Advisor
    Homework Helper

    open the file with fopen() and then use fscanf()
    if you search for these terms you should find lots of examples
     
  4. Oct 25, 2008 #3
    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.
     
  5. Oct 25, 2008 #4

    mgb_phys

    User Avatar
    Science Advisor
    Homework Helper

    If you just want the same code to read from a file you can just use:
    program << file.txt
     
  6. Oct 25, 2008 #5
    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?
     
  7. Oct 25, 2008 #6

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    Did you read the second post?
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: Very simple question about gcc (C compilation)
Loading...