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");
Files 433
if(fp==NULL)
(
printf ("File notfoundn");
return;
I
else
{
if (fgets(text,50,fp)!=NULL)
while (text[i)!= 0')
{
putchar(text[i’.]);
i++;
I
OUTPUT:
Enter File Name: IO.C
#include <stdio.h>
Explanation In the above program the function f gets () reads string from the file pointed by file
pointer. In this example i o . c file is opened and its first line is displayed on the screen.
8. f puts ( ) This function is useful when we want to write a string into the opened file.
13.14 Write a program to write a string into a file using fputcO function.
# include <stdio.h >
# include cconio.h>
void mainO
{
FILE *fp ;
char f i l e [1 2 ],t e x t [5 0];
c lr s c r O ;
printf ("n Enter the name of file:");
scanf ("% s", file);
fp=fopen(file,"w");
if(fp==NULL)
I
printf ("nFile can not openedn");
return ;
else
434 Programming and Data Structures
printf (" ' rt Enter Text Here: ");
scanf ("%s",iexty,
fputs(text,fp);
)
fcloseifpb
I
OUTFITS
Enter the name of file :data.dat
Enter Text H ere: Good Morning
Expto/MMon In the examftfe H a t * . d i t * file is opened in write mode. The text entered bv
the user is written in the file using f puts () function. The user can read the contents of file from
dos prompt or one can read the contents of file using f gets () function as explained in the
previous example.
9. putw ( ) This function is used to write an integer value to file pointed by file pointer.
13.15 Write a program to enter integers and write them in the file using putw() function.
# in clud* <std io.h>
# in clud* cc o n io .h>
# in clud* <proc«ss.h>
void aainO
{
FILS * fp j
in t v ;
c lr s c r ( ) ;
fp^fopen ( “num. txt", ”w ");
if (fp = xN U L L )
{
printf ("n File dose not exist");
exit(l);
{
printf ("n Enter Numbers :");
w hiled)
I
scanf r% d ",& v);
if (v ~ -0 )
I
fclose(fp);
exit(l);
i
Files 435
putw(v,fp);
}
OUTPUT:
Enter Num bers: 1 2 3 4 5 0
Explanation In the above program the file "num. tx t" is opened in write mode. Integers are then
entered and written in the file using putw () function. When the ' 0 ' is entered writing of data is
stopped and file is closed.
10. getw ( ) This function returns the integer value from a file and increases the file pointer.
13.16 Write a program to read integers from the file using getwO/function.
# include <stdio.h>
# include <conio.h>
# include <proce«s.h>
vo id mainO
{
KILE *fps
in t vj
c lr e c r ( ) ;
fp=fopen("num.txt "r ");
if (fp==NU L L )
{
printf ("n File does not exist");
exit(l);
i
else
/
printf ( Enter number:");
while ( (v=getw (fp))!=EOF)
printf ("% 2d",v);
fcloseifp);
)
OUTPUT:
12 345
Explanation In the above program the same file "num. tx t" is used in which previous program is
opened for reading the integers. Here, the function getw () reads integers from the file.
..................Content has been hidden....................

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