
420 Programming and Data Structures
Syntax
fp =fopen ( "d a ta . t x t " , "w ") ;
Here, data. txt is the file name and "w" is the mode.
2 . r (read) This mode opens a pre-existing file for reading. If the file doesn't exist, then compiler
returns NULL to the file pointer. Using pointer with i f statement we can prompt the user regarding
failure of operation.
Syntax
fp=fopen ("data.txt","r");
if (fp==NULL)
printf ("File does not exist");
OR
If (fp=(fopen ("data.txt","r"))==NULL)
printf ("File does not exist");
Here, data. dbf is opened for reading only. If the file does not exist the fopen () returns NULL to
file pointer x