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 f p '. Using the NULL value of f p with i f statement we can prompt the user for failure
of fopen () function.
A program is illustrated below giving the use of "w" and " r " modes.
13.1 Write a program to write data to text file and read it.
# include <stdio.h>
# include <conio.h>
# include <process.h:
void main()
{
FILB *fp j
char cm' *;
c lrs c r O ;
fp=fopen("da ta. txt", "w ”);
if(fp ==N UL L )
I
printf ("Cannot open file");
exit(l);
}
printf ("W rite data & to stop press
while (c!=.')
{
csgetcheO;
fputc(c,fp);
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.152.198