#include
#include
void main()
{
FILE *all,*even,*odd;
int number,i,records;
printf("INPUT THE TOTAL NUMBER OF RECORDS THAT U WANT TO ENTER
");
scanf("%d",& records);
all=fopen("ANYNUMBER","w");
for(i=1;i<=records;i++)
{
scanf("%d",&number);
if(number==-1)break;
putw(number,all);
}
fclose(all);
all=fopen("ANYNUMBER","r");
even=fopen("EVENNUMBER","w");
odd=fopen("ODDNUMBER","w");
while((number=getw(all))!=EOF)
{
if(number%2==0)
putw(number,even);
else
putw(number,odd);
}
fclose(all);
fclose(even);
fclose(odd);
even=fopen("EVENNUMBER","r");
odd=fopen("ODDNUMBER","r");
printf("
THE EVEN NUMBERS ARE");
while((number=getw(even))!=EOF)
printf(" %4d",number);
printf("
THE ODD NUMBERS ARE");
while((number=getw(odd))!=EOF)
printf(" %4d",number);
fclose(even);
fclose(odd);
}
#include
void main()
{
FILE *all,*even,*odd;
int number,i,records;
printf("INPUT THE TOTAL NUMBER OF RECORDS THAT U WANT TO ENTER
");
scanf("%d",& records);
all=fopen("ANYNUMBER","w");
for(i=1;i<=records;i++)
{
scanf("%d",&number);
if(number==-1)break;
putw(number,all);
}
fclose(all);
all=fopen("ANYNUMBER","r");
even=fopen("EVENNUMBER","w");
odd=fopen("ODDNUMBER","w");
while((number=getw(all))!=EOF)
{
if(number%2==0)
putw(number,even);
else
putw(number,odd);
}
fclose(all);
fclose(even);
fclose(odd);
even=fopen("EVENNUMBER","r");
odd=fopen("ODDNUMBER","r");
printf("
THE EVEN NUMBERS ARE");
while((number=getw(even))!=EOF)
printf(" %4d",number);
printf("
THE ODD NUMBERS ARE");
while((number=getw(odd))!=EOF)
printf(" %4d",number);
fclose(even);
fclose(odd);
}
sir i have one problem i the file handling program
ReplyDelete#include
#include
main()
{
FILE *fp,*fp1;
int i,sum=0;
clrscr();
fp=fopen("DATA.TXT","w");
for(i=1;i<=20;i++)
{
putw(i,fp);
}
putw(EOF,fp);
fclose(fp);
printf("\nData From the File\n");
fp=fopen("DATA.TXT","r");
fp1=fopen("ODD.TXT","w");
while((i=getw(fp))!= EOF)
{
printf("%d\t",i);
if(i%2 != 0)
sum = sum + i;
}
printf("Sum of only odd numbers is = %d",sum);
putw(sum,fp1);
fclose(fp);
fclose(fp1);
getch();
}
Data From the File
1 2 3 4 5 6 7 8 9 10
11 12 3584 3840 4096 4352 4608 4864 5120 -256
Sum of only odd numbers is = 36
Above program take garbage values after 12 as shown in the output. Can u Suggest the solution.
Anurag Dwivedi
anuragdwivediji@yahoo.com