Once you open a file for input, you must switch from scanf
to
fscanf
, an acronym for file scan field.
The fscanf
function takes an additional argument, a file pointer,
and reads from the file associated with the file pointer instead of from
your keyboard. Thus, unlike scanf
, fscanf
knows about the
structure variables in the structures created by fopen
, and
fscanf
uses that knowledge to get at the information in a file.
For example, the following statement tells C to read a floating-point
price and an integer number from the file identified by the
trade_source
pointer:
fscanf (trade_source, "%f%i", &price, &number);