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

  • Thread starter Thread starter Werg22
  • Start date Start date
  • Tags Tags
    Gcc
Click For Summary

Discussion Overview

The discussion revolves around how to redirect input for the `scanf` function in C to read from a file instead of standard user input. The scope includes practical coding techniques and command line usage.

Discussion Character

  • Exploratory
  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant inquires about modifying their code to read from a text file using `scanf`.
  • Another participant suggests using `fopen()` and `fscanf()` as a solution, indicating that examples can be found through further research.
  • A participant expresses a need to perform this operation from the command line, mentioning the use of the "<" symbol but not recalling the specifics.
  • Another suggestion is made to use the command `program << file.txt` to achieve the desired file reading functionality.
  • A participant asks about writing test cases for their program that reads from files, noting their usual method of using `assert` statements and inquiring about alternatives for file input.
  • One participant prompts another to consider the previous suggestion regarding `fscanf()`.

Areas of Agreement / Disagreement

Participants present multiple competing views on how to redirect input for `scanf`, with no consensus reached on a single method. The discussion remains unresolved regarding the best approach for file input redirection.

Contextual Notes

Some participants' suggestions depend on specific contexts, such as command line usage versus programmatic file handling, which may not be universally applicable.

Who May Find This Useful

Readers interested in C programming, particularly those looking to handle file input and output, as well as those developing test cases for file-based applications.

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?
 
Technology 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?
 
Did you read the second post?
 

Similar threads

Replies
14
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 27 ·
Replies
27
Views
6K
  • · Replies 29 ·
Replies
29
Views
4K
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 0 ·
Replies
0
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 20 ·
Replies
20
Views
5K