We make the following declaration before opening a file
FILE * fp
Now let us understand the following statements,
FILE * fp;
fp = fopen (“PR1.C”,”r”);
fp is a pointer variable which contains address of the structure FILE which has been defined in the header file “stdio.h”.
fopen( ) will open a file “PRI.C” in read mode.
(i) Firstly it searches on the disk the file to be opened.
(ii) If the file is present, it loads the file from the disk in to memory. Of course if the file is very big, then it loads the file part by part. If the file is absent, fopen( ) returns a NULL. NULL is a macro defined in “stdio.h” which indicates that you failed to open the file.
(iii) It sets up a character pointer (which is part of the FILE structure) which points to the first character of the chunk of memory where the file has been loaded.
# include "stdio.h"
main( )
{
FILE *fp;
fp = fopen(“PRI.C”, “r”);
if (fp= = NULL)
{
puts (“ cannot open file”);
exit( );
}
}
FILE * fp
Now let us understand the following statements,
FILE * fp;
fp = fopen (“PR1.C”,”r”);
fp is a pointer variable which contains address of the structure FILE which has been defined in the header file “stdio.h”.
fopen( ) will open a file “PRI.C” in read mode.
fopen( ) performs three important tasks when you open the file i “r” mode:
(i) Firstly it searches on the disk the file to be opened.
(ii) If the file is present, it loads the file from the disk in to memory. Of course if the file is very big, then it loads the file part by part. If the file is absent, fopen( ) returns a NULL. NULL is a macro defined in “stdio.h” which indicates that you failed to open the file.
(iii) It sets up a character pointer (which is part of the FILE structure) which points to the first character of the chunk of memory where the file has been loaded.
# include "stdio.h"
main( )
{
FILE *fp;
fp = fopen(“PRI.C”, “r”);
if (fp= = NULL)
{
puts (“ cannot open file”);
exit( );
}
}