There are different operations that can be carried out on a file. These are :
1. Creation of a new file
2. Opening an existing file
3. Reading from a file
4. Writing to a file
5. Moving to a specific location in a file (seeking)
6. Closing a file
Let us now write a program to read a file and display its contents on the screen. We will first list the program and show what it does, and then dissect it line by line.
\* C Program to Display contents of a file on screen *\
# include < stdio.h >
int main( )
{
FILE *fp ;
char ch ;
fp = fopen(" File.c ", " r ") ;
while ( 1 )
{
fp = fgetc ( fp ) ;
if ( ch == EOF )
break ;
printf(" %c ", ch) ;
}
fclose(fp) ;
return ( 0 ) ;
}
On execution of this program it displays the contents of the file ‘File.c’ on the screen.