C Programming

File Handling – Separate Even, Odd numbers

Accept 10 numbers and separate Even,Odd numbers and write them into two separate files using File Handling in C.

Language : C

[sourcecode language='c']
//To make a file and separate even and odd numbers
#include
void main()
{ int i,a;
char ch;
FILE *fp, *even, *odd;
fp=fopen(“Num”, “w+”);
even=fopen(“even”, “w+”);
odd=fopen(“Odd”, “w+”);

printf(“Enter a list of 10 numbers : “);
for(i=0;i<10;i++)
{
scanf(“%d”, &amp;amp;a);
putw(a,fp);
}

rewind(fp);
while(1)
{ a=getw(fp);

if(a==EOF)
break;
if(a%2==0)
putw(a,even);
else
putw(a,odd);
}

do{

printf(“1.Read even nos\n2.Read odd nos : “);
scanf(“%d”, &amp;amp;a);

rewind(even);
rewind(odd);
switch(a)
{
case 1:
while(1)
{ a=getw(even);
if(a==EOF)
break;
printf(“\t%d”, a);
}
fclose(even);
break;

case 2:
while(1)
{ a=getw(odd);
if(a==EOF)
break;
printf(“\t%d”, a);
}

fclose(odd);
break;
}

printf(“\nMore Actions (Y/N)”);
fflush(stdin);
scanf(“%c”, &amp;amp;ch);
}while(ch==’y'||ch==’Y');
}
[/sourcecode]



{ 1 comment… read it below or add one }

1 sanjay s January 29, 2011 at 12:23 pm

write a program to create a file doubly linked list. for ex, initially create file say file1.txt it should consists of random number values & two null values create one more file say file2.txt with either random value with name of previous file file1.txt program should have following functions.a)create a new file & insert it after any nth file b) delete any file from list c) display the content of all the files

Reply

Leave a Comment

Notify me of followup comments via e-mail. You can also subscribe without commenting.


WordPress - Vaibhav Kanwal