430 Programming and Data Structures
Explanation In the above program the ge tc () reads character from the file " li s t . txt". Some text is
to be written before reading this file.
4. putc ( ) This function is used to write a single character into a file. If an error occurs it returns EOF.
13.10 V/rite a program to write some text into the file using putcO function.
# include <stdio.h>
# include <conio;h>
void mainO
{
int c;
FILE * fp ;
clrscr ();
printf ("n Enter Few Words to Exitn");
fp=fopen("words.doc", ”w ");
while ( (c=getchari))!='*')
putc(c,fp);
fcloseifp);
}
OUTPUT:
Enter Few Words to Exit
This is saved into the file *
Explanation The putc () function writes character reads through getchar () in the file "words. doc".
The user should enter ' * ' to stop reading character.
5. f g e tc ( ) This function is similar to g e t c () function. It also reads a character and increases the
file pointer position. If any error or end of file is reached it returns EOF.
13.11 Write a program to read a 'C' program file and count the following in the complete
program.
Ibtal number of statements.
Total number of included files.
Total number of blocks & brackets.
#include <stdio.h>
finclude <conio.h>
nain()
<
PILE * f s ;
int i*0/x /y,CBO,sb>0,b>>0;
clrscr()j
fs=fopen(“PRG2. C", "r");
if(fs=**NILL)