0% found this document useful (0 votes)
29 views

The Fprintf & Fscanf Functions

The document discusses four file input/output functions in C: getc(), putc(), fprintf(), and fscanf(). It explains that getc() reads a single character from a file while putc() writes a single character to a file. fprintf() writes data to a file similarly to printf() but uses a file pointer, and fscanf() reads data from a file like scanf() but also uses a file pointer. It then provides an example of defining an array of structures to store student data for multiple students.

Uploaded by

Naveen Syam
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

The Fprintf & Fscanf Functions

The document discusses four file input/output functions in C: getc(), putc(), fprintf(), and fscanf(). It explains that getc() reads a single character from a file while putc() writes a single character to a file. fprintf() writes data to a file similarly to printf() but uses a file pointer, and fscanf() reads data from a file like scanf() but also uses a file pointer. It then provides an example of defining an array of structures to store student data for multiple students.

Uploaded by

Naveen Syam
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

getc() Reads a character from a file

putc() Writes a character to a file

fprintf() Writes a set of data values to a file

fscanf() Reads a set of data values from a file

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).

The fprintf & fscanf functions:


The fprintf and fscanf functions are identical to printf and scanf functions except that
they work on files. The first argument of theses functions is a file pointer which specifies
the file to be used. The general form of fprintf is

fprintf(fp,”control string”, list);

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. 

You might also like