DEPARTMENT OF TECHNICAL EDUCATION ANDHRA PRADESH

Name Designation Branch Institute Year/Semester Subject Subject Code Topic Duration Sub Topic Teaching Aids :M.Subramanyam :Senior Lecturer :Computer Engg. :Q.Q.Govt.Polytechnic,Hyderabad. :III Semester :UNIX & C :CM-304 :Files :50 Min :File I/O functions :PPTs, Animations
CM304.86 1

Recap
 What is the use of fgetc() and fputc()?  What is the use of fgets() and fputs()?  What is the use of getw() and putw()?  Can you write different types of values to a file
using single function(fputc(),fputs(),putw())?

 Can you read different types of values from a
file using single function(fgetc(),fgets(),getw())?
CM304.86 2

Objectives
On completion of this period, you would be able to know …

 Read different types of values from the file.  Write different types of values to the file.  Write a structure to the file.  Read a structure from the file.
CM304.86 3

Formatted file I/O
 fprintf(): To write multiple type of data values into
the file.  Sends formatted data to the file. Syntax: fprintf(FILE *,”conversionspecifiers”,variable(s))  Conversion specifiers - %c, %d, %f etc.,  Variable(s)-variables whose values are to be written to the file

CM304.86

4

Formatted file I/O
Contd..

Example:

 char a = ‘Z’; int b = 10; float c = 3.17;  FILE * fp;  fprintf (fp,”%c%d%f”,a,b,c);

CM304.86

5

Example

CM304.86

6

Example
 fscanf(): To read character, string, integer, float etc.,
from the file.

 Reads formatted data from the file.  Syntax:
fscanf (FILE *,”conversionspecifiers”,&variable(s))

 Conversion specifiers-%c, %d, %f etc.,  &Variable(s)-addresses of the variables whose
values are to be read from the file.
CM304.86 7

Example
Example:

 char a; int b; float c;  FILE * fp;  fscanf(fp,”%c%d%f”,&a,&b,&c);

CM304.86

8

Example

CM304.86

9

Structure data file I/O
 Structure can be read from file using fread().  Structure can be written to file using fwrite().  fread() and fwrite are also known as binary file
I/O functions.

CM304.86

10

fread() function
 Reads a specified number of bytes from a file
into memory.

 Used to write an entire structure block to a
given file.

 fread( &structurevariable, sizeof
(structurevariable), 1, FILE * );
CM304.86 11

fread() function
Contd..

 &structurevariable – Location to transfer bytes
from file.

 sizeof( structurevariable ) – Number of bytes
to transfer .

 1 – number of elements of an array/records to
transfer.

 In case of an array, "one element" of an
array is being transferred.

 FILE pointer – File to transfer from
CM304.86 12

fwrite() function
 Transfer bytes from a location in memory to a
file.

 Write an entire structure block to a given file.  fwrite( &variable, sizeof( datatype ), 1, FILE * );  &variable – Location to transfer bytes from
structure to file.
CM304.86 13

fwrite() function
Contd..

 1 – number of elements of an array/records
to transfer

 sizeof( structure ) – Number of bytes to
transfer.

 In case of an array, "one element" of an
array is being transferred

 FILE Pointer - File to write.
CM304.86 14

Example

CM304.86

15

Example

Contd..

CM304.86

16

Summary
In this period, you have learnt about…

 Different types of values can be read from the file
using fscanf().

 Different types of values can be written to the file
using fprintf().

 Structures can be read from the file using fread().  Structures can be written to the file using fwrite().
CM304.86 17

Quiz
1)fprintf() writes data to a)file b)keyboard c)both (a) and (b) d)none

CM304.86

18

Quiz
1)fprintf() writes data to a)file b)keyboard c)both (a) and (b) d)none

CM304.86

19

Quiz
2)Which of the following function is used for reading binary data from the file a) read() b) fread() c) fscanf() d) all the above
CM304.86 20

Quiz
2)Which of the following function is used for reading binary data from the file a) read() b) fread() c) fscanf() d) all the above
CM304.86 21

Frequently Asked Questions
1.Distinguish between scanf() and fscanf(). 2.Distinguish between printf() and fprintf(). 3.Explain fread() and fwrite() with their syntax.

CM304.86

22