printf()
and
scanf()
that will work on
any file, not just the standard input and standard output
ones.
They are called
fprintf()
and
fscanf()
and we use them only
on text files.
Suppose we wanted to put a line of text into a file that
contained the name, ID number and GPA of student number
i
where each student is in a structure.
We could use a statement like:
fprintf(fp, "%s %d %f\n", students[i].name, students[i].id, students[i].gpa);
fscanf(fp, "%d%d", &x, &y);
The basic use of
fprintf()
and
fscanf()
is just as it is for
printf()
and
scanf()
.
We just have to remember to put the file pointer variable as the
first argument.
As a side note, in some implementations,
printf(...)
is just
a shorthand for
fprintf(stdout, ...)
.
They really use the same code.