fopen( ) in C

fopen() function in C accepts two arguments as strings. The first argument denotes the name of the file to be opened and the second signifies the mode in which the file is to be opened. The second argument can be any of the following:

File ModeDescription
rOpen a text file for reading
wCreate a text file for writing, if it exists, it is overwritten.
aOpen a text file and append text to the end of the file.
rbOpen a binary file for reading
wbCreate a binary file for writing, if it exists, it is overwritten.
abOpen a binary file and append data to the end of the file.
fopen( ) second arguments

Example of fopen() function

# include "stdio.h" 
main( ) 
{ 
 FILE *fp ; 
 char ch ; 
 
 fp = fopen ( "PR1.C", "r" ) ; 
 while ( 1 ) 
 { 
 ch = fgetc ( fp ) ; 
 if ( ch == EOF ) 
 break ; 
 printf ( "%c", ch ) ; 
 } 
 fclose ( fp ) ; 
} 

On execution of this program it displays the contents of the file ‘PR1.C’ on the screen

Leave a Comment