432 Programming and Data Structures
6. f pu cc ( ) This function writes the character to the file shown by the file pointer. It also increases the
file pointer.
Syntax
f pu tc (c , f p ) where, f p is file pointer and c is a variable written to the file pointed by file pointer.
13.12 Write a program to write text to a file using fputcO function.
# include <stdio.h>
# include <conio.h>
void main ()
{
FILE *fp ;
char c;
c l r s c r ( ) ;
fp=fopen(" lines. txt", "w ");
if(fp ==N U LL )
return;
else
while ( <c=getche())!='*')
fputc(c,fp);
I
fclose(fp);
I
QUXPUT;
India is my country.
Explanation In the above program the text entered by the user is written into the file "lin es • txt"
using fputc () function.
7. f g e t s ( ) This function reads string from a file pointed by file pointer. It also copies the string to
a memory location referred by an array.
13.13 Write a program to reaid text from the given file using fgetsO function.
#include <stdio.h>
♦include <conio.h>
void main ()
{
FILE *fp;
char f i l e [2 0 ], t e x t [5 0 ];
int i«0;
printf ("Enter File Name:");
scanf ("%s",file);
fp=fopen(file, "r");