The Fprintf & Fscanf Functions
The Fprintf & Fscanf Functions
The getc and putc functions are analogous to getchar and putchar functions and
handle one character at a time. The putc function writes the character
contained in character variable c to the file associated with the pointer fp1. ex
putc(c,fp1); similarly getc function is used to read a character from a file that
has been open in read mode. c=getc(fp2).
Where fp id a file pointer associated with a file that has been opened for writing. The
control string is file output specifications list may include variable, constant and string.
fprintf(f1,%s%d%f”,name,age,7.5);
Here name is an array variable of type char and age is an int variable
The general format of fscanf is
fscanf(fp,”controlstring”,list);
This statement would cause the reading of items in the control string.
fscanf(f2,”5s%d”,item,&quantity”);
Like scanf, fscanf also returns the number of items that are successfully read.
Arrays of structure:
It is possible to define a array of structures for example if we are maintaining
information of all the students in the college and if 100 students are studying in the
college. We need to use an array than single variables. We can define an array of
structures as shown in the following example:
structure information
{
int id_no;
char name[20];
char address[20];
char combination[3];
int age;
}
student[100];
An array of structures can be assigned initial values just as any other array can.
Remember that each element is a structure that must be assigned corresponding initial
values as illustrated below.