You are on page 1of 11

File Handling

10.1. INTRODUCTION
Storage of data in variables, arrays and structures are temporary, all such
data is lost when the program terminates. Many applications require that
information to be read from or written on an auxiliary memory device. Such
information is stored in the memory device in the form of a file. Files are used for
permanent storage of large amount of data. A file is a place on the disk. n many
programming situations, it is easier to access a file than reading data from a
keyboard. A file can be used to write and or read from a disk. File handling in ! is
very simple, simple it essentially treats a file as "ust as a stream of characters and
allows input and output in a file.
#he basic operations performed on a file includes $
• %aming a file.
• &pening a file.
• 'riting data into a file.
• (eading data from a file.
• !losing a file.
10.2. DEFINING A FILE
File handling in ! is very simple, as it essentially treats a file as "ust a stream
of characters and accordingly allows input and output as a stream of characters. f
we want to store data in a file we have to create a buffer area. #his buffer area
allows information to be read from or written on a data file. #he buffer area is
automatically created as soon as the file is declared.
#here is a special data structure called F)* type+defined in the header file
,stdio.h-. All file .& operations utili/e this data structure. #his structure has fine
fields to store all relevant information concerning the file to which it is associated.
#he various flags for errors in reading and writing files are also maintained in F)*
structure.
n a ! program, all files are referred through file pointers. A file pointer is a
pointer variable of the type F)*. #he syntax for defining a file pointer variable
fptr is as follows $
FILE *fptr ;
10.2 File Handling
'here F)* is a defined data type. All files should be declared as type F)*
before they are used. #he data type F)* should be compulsorily written in
capitals. #he file pointer fptr is referred to as the stream pointer. #his pointer
contains all the information about the file, which is subsequently used as a
communication link between the system and the program.
10.3. OPENING A FILE
*very file must be opened before the program can perform any operation on
it. #he library function fopen01 is utili/ed for this purpose. #he syntax for a call to
this function typically will look like as follows $
fptr = fopen(“filename”, “mode”) ;
2efore such a call is made, the variable fptr should have been defined as
follows $
FILE *fptr ;
#he fopen01 function takes two arguments, a string of characters for the
name of the file and a string of character for the mode of use of the file.
Mode Meaning
r &pen for reading only.
w !reate for writing. f a file by that name already exists, it
will be overwritten.
a Append, open for writing at end of file, or create for writing
if the file does not exist.
r3 &pen an existing file for update 0reading and writing1
w3 !reate a new file for update 0reading and writing1. f a file
by that name already exists, it will be overwritten.
a3 &pen for append, open for update at the end of the file, or
create if the file does not exist.
#o specify that a given file is being opened or created in text mode, append
4t4 to the string 0rt, w3t, etc.1. #o specify binary mode, append 4b4 to the string
0wb, a3b, etc.1. fopen and 5fsopen also allow the 4t4 or 4b4 to be inserted
between the letter and the 3 character in the string. For example, rt3 is
equivalent to r3t. For example $
fp1 = fpen!"#a$%&.da'() "$(* +
fp2 = fpen!"$ep$'.da'() ",(* +
#he File ,marks.dat- is opened for reading and ,report.dat- is opened for
writing. f the ,report.dat- file already exists, its contents are deleted and the file
is opened as new. f ,marks.dat- file does not exist, an error will occur.
File Handling 10.3
10.-. CLO.ING A FILE
*very file opened must be closed when the program no longer requires it.
#his is accomplished by using the function fclose01, which has the following
syntax$
fclose(fptr) ;
'here fptr is the file pointer variable associated with the file that is to be
closed. #he fclose function returns %6)) for a successful close and *&F or 78 for
an unsuccessful close. For example $
fl&e!fp2* +
f/le!fp1* +
'ill close the files, which are opened. &nce a file is closed, its file pointer can
be reused for another file.
N'e 0 All files are closed automatically whenever a program terminates.
10.1. CHARACTER I2O FUNCTION.
&nce a file is opened, an .& operation is performed on files. Some of the
functions which facilitate single character .& from files are fgetc01 and fputc01.
#hese functions are similar to fgetchar01 and fputchar01 are character .&
functions which act on files.
fge'/!* F3n/'in
#his function is used to read a character from the current position of the file
associated with the file pointer. #he current position is incremented by one after
this operation. #he character read is returned if there is no error. #his receives a
single argument, a file pointer for the file from which a character is read. #he
syntax is $
variable = fgetc(fptr) ;
'here variable is any character variable, which will store the characters, read
from the file. f the end of file is encountered during reading, fgetc01 returns an
end of file marker. So it is always advisable to check for the return value of this
function for *&F i.e. 78.
ge'/!* F3n/'in
#his function is implemented as a macro to read a character from the file. t is
identical with fgetc01 in syntax and in performance.
10.4 File Handling
fp3'/!* F3n/'in
#his function writes a character to a file, which is opened, for writing. #he
syntax is $
fptc(c,fptr) ;
'here c is the character to be written on to the file, fptr is the file pointer
associated with the file. #he function writes in current position of the file.
#hereafter the current position is incremented by one. f there is an error, it
returns *&F.
p3'/!* F3n/'in
#he macro putc01 function is an equivalent to fputc01 function. #his also
performs the same task as fputc01. #hese can be used interchangeably.
#he following program illustrates the use of character .& functions $
24 Ill3&'$a'in f /5a$a/'e$ I2O f3n/'in& 42
6 in/l3de 7&'di.58
6 in/l3de 7/ni.58
9id #ain!*
:
/5a$ / +
FILE 4fp'$1 +
/l$&/$!* +
p$in'f!;En'e$ '5e 'e<' ' =e &'$ed in '5e file.>n;* +
p$in'f!;U&e ?@ $ FA a' '5e end and p$e&& ENTER0 >n>n;* +
fp'$1 = fpen!;.UBCECT.DAT;);,;* +
,5ile!!/ = ge'/!&'din** D= EOF*
fp3'/!/) fp'$1* +
f/l&e!fp'$1* +
p$in'f!;>nT5e /n'en' f '5e file i& 0 >n>n;* +
fp'$1 = fpen!;.UBCECT.DAT;) ;$;* +
d
:
/ = fge'/!fp'$1* +
p3'/5a$!/* +
E ,5ile!/ D= EOF* +
f/l&e!fp'$1* +
ge'/5!* +
E
File Handling 10.5
RUN 1 0
En'e$ '5e 'e<' ' =e &'$ed in '5e file.
U&e ?@ $ FA a' '5e end and p$e&& ENTER0
C#p3'e$ P$a/'i/e F II
F$ EEE) ECE G HECH
?@
T5e /n'en' f '5e file i& 0
C#p3'e$ P$a/'i/e F II
F$ EEE) ECE G HECH
10.A. .TRING I2O FUNCTION.
&nce a file is opened, an .& operation is performed on files. Some of the
functions which facilitate string .& from files are fgets01 and fputs01. #hese
functions are similar to gets01 and puts01 are string .& functions which act on
files.
fge'&!* F3n/'in
#he fgets01 is used to read a string from a file opened for reading. #his
function requires the si/e of the destination array as an argument. Fgets01 reads
characters from fptr and stops when it reads either n + 8 characters or a new line
character, whichever comes first. #he syntax is $
fgets(destination, b!tes, fptr) ;
'here destination is the name of the array into which the string will be read,
bytes is the number of bytes i.e. the maximum number of characters to be read
from the file and fptr is the return value of the fopen01 function.
fgets01 function can also be used to read information from the keyboard. #his
is achieved by replacing the file pointer with stdin, which denoted the standard
input device i.e. keyboard.
fp3'&!* F3n/'in
#he fputs01 function is used to write a string to a file which is opened for
writing. t does not append a new line character, and the terminating null
character is not copied. #he syntax is $
fpts(string, fptr) ;
10.6 File Handling
'here string is the string to be written to the file and the file pointer is the
return value of the fopen01 function.
fputs01 function can also be used to write information to the monitor. #his is
achieved by replacing the file pointer with stdout, which denoted the standard
output device i.e. 9:6.
#he program below explains the fgets01 and fputs01 functions $
24 Ill3&'$a'in f &'$ing I2O f3n/'in& 42
6 in/l3de 7&'di.58
6 in/l3de 7/ni.58
9id #ain!*
:
/5a$ 4/ +
FILE 4fp'$1 +
/l$&/$!* +
fp'$1 = fpen!;C.E.DAT;);,;* +
p$in'f!;En'e$ '5e /n'en' f file 0 >n>n;* +
ge'&!/* +
fp3'&!/) fp'$1* +
f/l&e!fp'$1* +
p$in'f!;>nT5e /n'en' f '5e file i& 0 >n>n;* +
fp'$1 = fpen!;C.E.DAT;) ;$;* +
,5ile!fge'&!/) &'$len!/* I 1) fp'$1* D= NULL*
:
p3'&!/* +
E
f/l&e!fp'$1* +
ge'/5!* +
E
RUN 1 0
En'e$ '5e /n'en' f file 0
C#p3'e$ ./ien/e and Enginee$ing
T5e /n'en' f '5e file i& 0
C#p3'e$ ./ien/e and Enginee$ing
File Handling 10.7
10.J. INTEGER I2O FUNCTION.
ge',!* F3n/'in
#he getw01 function is a integer oriented function. #his is similar to getc01
function is used to read integer values. #his function would be useful when we
deal with only integer data. getw returns the next integer in the named input fptr.
#he syntax is $
get"(fptr) ;
t assumes no special alignment in the file. getw should not be used when the
stream is opened in text mode.
p3',!* F3n/'in
#he putw01 function is also a integer oriented function. #his is similar to
putc01 function is used to write integer values. #his function would be useful when
we deal with only integer data. putw outputs the integer to the given fptr. t does
not expect 0and does not cause1 special alignment in the file. #he syntax is $
pt"(fptr) ;
#he program below reads integer numbers and store in a file name
,%6M2*(S.:A#-, read these numbers and then write all odd numbers to a file to
be called ,&::.:A#- and all even numbers to a file to be called ,*9*%.:A#-.
24 Ill3&'$a'in f In'ege$ I2O f3n/'in& 42
6 in/l3de 7&'di.58
6 in/l3de 7/ni.58
9id #ain!*
:
FILE 4fp'$1) 4fp'$2) 4fp'$3 +
in' n) i) n3# +
/l$&/$!* +
p$in'f!;En'e$ n3#=e$ f 9al3e& 0 ;* +
&/anf!;Kd;) Gn* +
p$in'f!;>nEn'e$ '5e 9al3e& 0 ;* +
fp'$1 = fpen!;NUHBER..DAT;) ;,;* +
f$!i = 0 + i 7 n + iII*
:
&/anf!;Kd;) Gn3#* +
p3',!n3#) fp'$1* +
E
f/l&e!fp'$1* +
10.8 File Handling
fp'$1 = fpen!;NUHBER..DAT;) ;$;* +
fp'$2 = fpen!;ODD.DAT;) ;,;* +
fp'$3 = fpen!;ELEN.DAT;) ;,;* +
,5ile!!n3# = ge',!fp'$1** D= EOF*
:
if!n3# K 2 == 0*
p3',!n3#) fp'$3* +
el&e
p3',!n3#) fp'$2* +
E
f/l&e!fp'$1* +
f/l&e!fp'$2* +
f/l&e!fp'$3* +
fp'$2 = fpen!;ODD.DAT;) ;$;* +
fp'$3 = fpen!;ELEN.DAT;) ;$;* +
p$in'f!;>nCn'en'& f ODD file i& 0 ;* +
,5ile!!n3# = ge',!fp'$2** D= EOF*
p$in'f!;Kd>';) n3#* +
p$in'f!;>n>nCn'en'& f ELEN file i& 0 ;* +
,5ile!!n3# = ge',!fp'$3** D= EOF*
p$in'f!;Kd>';) n3#* +
f/l&e!fp'$2* +
f/l&e!fp'$3* +
ge'/5!* +
E
RUN 1 0
En'e$ n3#=e$ f 9al3e& 0 A
En'e$ '5e 9al3e& 0 11 22 33 -- 11 AA
Cn'en'& f ODD file i& 0 11 33 11
Cn'en'& f ELEN file i& 0 22 -- AA
10.M. FORHATTED I2O FUNCTION.
#he functions scanf01 and printf01 are used for performing .& operations in
programs without involving files. Similarly fscanf i.e., ,file scanf- and fprintf01 i.e.,
,file printf- are used to perform .& operations in files. #he first argument of these
functions is a file pointer, which specifies the file to be used.
File Handling 10.9
f&/anf!* F3n/'in
#he parameters of fscanf01 function is same as scanf01 function except that
there is one additional parameter i.e., the file pointer associated with the file from
which the input is read. #his statement would cause the reading of the items in
the list from the file specified by fptr, according to the specifications contained in
the control string. #he syntax $
fscanf(fptr, “control string”, list) ;
'here fptr is a file pointer associated with a file that has been opened for
reading. #he control string contains input specifications for the items in the list.
#he list may include constants and strings. *xample $
f&/anf!fp1) "Kd K&() G$lln) na#e* +
'here rollno is an int variable and name is an array variable of type char. #he
above statement reads the rollno and name of the student from the file pointed
by the file pointer fp8.
fp$in'f!* F3n/'in
#he parameters of fprintf01 function is same as printf01 function except that
there is one additional parameter i.e., the file pointer associated with the file from
which the output is sent. #his statement would cause the writing of the items in
the list to the file specified by fptr, according to the specifications contained in the
control string. #he syntax $
fprintf(fptr, “control string”, list) ;
'here fptr is a file pointer associated with a file that has been opened for
writing. #he control string contains output specifications for the items in the list.
#he list may include constants and strings. *xample $
fp$in'f!fp2) "Kd K&() $lln) na#e* +
'here rollno is an int variable and name is an array variable of type char.
#he above statement writes the rollno and name of the student to the file pointed
by the file pointer fp;.
!onsider the following program, here the data for student<s roll number, name
and marks in two sub"ects are read and it is then written to the file
,S#6:*%#.:A#- that is being pointed by the file pointer fptr. After closing the file
,S#6:*%#.:A#-, it is again reopened for reading and the total marks of each
student are calculated and displayed.
10.10 File Handling
24 Ill3&'$a'in f f$#a''ed I2O f3n/'in& 42
6 in/l3de 7&'di.58
6 in/l3de 7/ni.58
9id #ain!*
:
FILE 4fp'$ +
in' i) n) $lln) &1) &2 +
/5a$ na#eN10O +
/l$&/$!* +
fp'$ = fpen!;.TUDENT.DAT;) ;,;* +
p$in'f!;En'e$ '5e n3#=e$ f &'3den'& 0 ;* +
&/anf!;Kd;) Gn* +
f$!i = 0 + i 7 n + iII*
:
p$in'f!;>nEn'e$ '5e $ll n3#=e$ 0 ;* +
&/anf!;Kd;) G$lln* +
p$in'f!;>nEn'e$ '5e na#e 0 ;* +
&/anf!;K&;) na#e* +
p$in'f!;>nEn'e$ '5e #a$%& in 2 &3=Pe/'& 0 ;* +
&/anf!;Kd Kd;) G&1) G&2* +
fp$in'f!fp'$) ;Kd K& Kd Kd >n;) $lln) na#e) &1) &2* +
E
f/l&e!fp'$* +
fp'$ = fpen!;.TUDENT.DAT;) ;$;* +
p$in'f!;>nRll N. Na#e >'>' .3=1 >' .3=2 >' T'al>n>n;* +
f$!i = 0 + i 7 n + iII*
:
f&/anf!fp'$);Kd K& Kd Kd >n;) G$lln) na#e)G&1)G&2* +
p$in'f!;Kd >' K& >'>' Kd >' Kd >' Kd >n;) $lln) na#e)
&1) &2) &1 I &2* +
E
f/l&e!fp'$* +
ge'/5!* +
E
RUN 1 0
En'e$ '5e n3#=e$ f &'3den'& 0 2

En'e$ '5e $ll n3#=e$ 0 101

En'e$ '5e na#e 0 RaP

En'e$ '5e #a$%& in 2 &3=Pe/'& 0 J0 J1
File Handling 10.11
En'e$ '5e $ll n3#=e$ 0 102
En'e$ '5e na#e 0 Q3#a$
En'e$ '5e #a$%& in 2 &3=Pe/'& 0 A0 A1
Rll N. Na#e .3=1 .3=2 T'al
101 RaP J0 J1 1-1
102 Q3#a$ A0 A1 121