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 Mode | Description |
---|---|
r | Open a text file for reading |
w | Create a text file for writing, if it exists, it is overwritten. |
a | Open a text file and append text to the end of the file. |
rb | Open a binary file for reading |
wb | Create a binary file for writing, if it exists, it is overwritten. |
ab | Open a binary file and append data to the end of the file. |
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