getw() and putw()
functions:
getw()
and putw() are number oriented functions are used to read and write
integer values on a given file.
getw() function: getw()
function is used to read an integer value from a given file. The general format of the getw() function is:
Syntax: getw(FilePointer);
This function receives FilePointer as an
argument and returns next integer from the input file. It returns EOF whenever end of file has been
reached.
putw() function: putw()
function is used to write an integer value into the specified file. The general format of the putw() function is:
Syntax: putw(N,FilePointer);
Where,
N
is an integer value to be written into the given file with FilePointer opened
in write mode.
/* WRITE A PROGRAM TO CREATE AN INPUT DATA FILE
WHICH CONTAINS A LIST OF INTEGERS. READ
THE SAME DATA FROM THE FILE AND WRITE EVEN AND ODD NUMBERS INTO TWO SEPARTE
FILES */
#include<stdio.h>
#include<conio.h>
main()
{
int
item,n,i;
FILE
*f1,*f2,*f3;
clrscr();
f1=fopen("input.dat","w");
printf("\nEnter
how many numbers:");
scanf("%d",&n);
printf("\nEnter
%d Numbers:",n);
for(i=1;i<=n;i++)
{
scanf("%d",&item);
putw(item,f1);
}
fclose(f1);
f1=fopen("input.dat","r");
if(f1==NULL)
{
printf("\nFILE
OPENING ERROR");
exit();
}
f2=fopen("even.dat","w");
f3=fopen("odd.dat","w");
while((item=getw(f1))!=EOF)
{
if(item%2==0)
putw(item,f2);
else
putw(item,f3);
}
fcloseall();
printf("\nEVEN
NUMBERS ARE:");
f2=fopen("even.dat","r");
while((item=getw(f2))!=EOF)
printf("%6d",item);
fclose(f2);
printf("\nODD
NUMBERS ARE:");
f3=fopen("odd.dat","r");
while((item=getw(f3))!=EOF)
printf("%6d",item);
fclose(f3);
}
No comments:
Post a Comment