Files 427
while (Ifeoflfp))
printf ("%c",getcifp));
I
Explanation This program is the same as the one explained earlier and the only difference is that the
file-opening mode is binary
3. ab (append) This mode opens a binary file in append mode i.e. data can be added at the end
of file.
Example
fp=fopen( "d a ta . d a t" , "a b " ) ;
Here, "d a ta . dat" file is opened in append mode.
4. r+b (read + write) This mode opens a pre-existing file in read and write mode i.e. file can be
read and written.
Example
fp = fo p e n ("d a t a .d a t ","r + b " );
Here, the file "d a ta . dat" is opened for reading and writing in binary mode.
5. w+b (read + w rite ) This mode creates a new file in read and write mode.
Exam ple
fp=fopen( "d a ta .d a t "#"w+b");
Here, the file "da ta. dat" is created for reading and writing in binary mode.
6. a+b (append+ w rite ) This mode opens a file in append mode i.e. data can be written at the
end of file. If file does not exist a new file is created.
Example
fp = fo p e n ("d a t a .d a t","a + b ");
Here, the file "D ata. dat" is opened in append mode and data can be written at the end of file.
b. Reading a file Once the file is opened using f open ( ) , it's contents are loaded into the memory
(partly or wholly). The pointer points to the very first character of the file. The f getc () function is
used to read the contents of the file. The syntax for f getc () is as follows.
c h = fg e tc (fp );
Where, f ge tc () reads the character from current pointer position and advances the pointer position
so that the next character is pointed. The variable ch contains the character read by f ge tc ( ) . There are
also other functions to read the contents of file, which are explained in next pages.
C. Closing a File The file that is opened from the f open () should be closed after the work is over
i.e. we need to close the file after reading and writing operations are over.
Example
flco s e ( f p ) ;
This statement closes the file associated with file pointer f p. This function closes one file at a time. To
close one or more files at a time the function f c lo s e a ll () is used.