You are on page 1of 3

PRACTICAL SET-10

10.1.Write a program to write a string in file.

PROGRAM:

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

int main()

Char sentence[1000]

FILE *fp1;

clrscr():

fp1= fopen ("program1.txt","r");

if(fp1==NULL)

printf("Error!");

gets(sentence);

printf("Enter a sentence:");

fprintf(fp1, "%s",sentence);

fclose(fp1);

return0;

10.2.A file name data contains series of integer numbers.write a c program to read all
numbers from file & then write all odd numbers into file named "odd" & write all
even numbers into file named "even" .Display all the the contents of these file on screen.
PROGRAM:

#include<stdio.h>
#include<conio.h>
#include<process.h>

void main()
{
int a,n,i;
FILE *fp1,*fp2,*fp3;
clrscr();
fp1=fopen("DATA","w");

if(fp1==NULL)
{
printf("File could not open!!");
exit(0);
}

printf("How many numbers? ");


scanf("%d",&n);
printf("Enter contents of DATA file:n");

for(i=0;i<n;++i)
{
scanf("%d",&a);
putw(a,fp1);
}

fclose(fp1);

fp1=fopen("DATA", "r");
fp2=fopen("ODD","w");
fp3=fopen("EVEN","w");

if(fp1==NULL||fp2==NULL||fp3==NULL)
{
printf("File could not open!!");
exit(0);
}

while((a=getw(fp1))!=EOF)
{
if(a%2!=0)
putw(a,fp2);
else
putw(a,fp3);
}

fclose(fp1);
fclose(fp2);
fclose(fp3);

fp2=fopen("ODD", "r");
fp3=fopen("EVEN","r");

if(fp2==NULL||fp3==NULL)
{
printf("File could not open!!");
exit(0);
}

printf("nContents of ODD file:n");


while((a=getw(fp2))!=EOF)
printf("%d",a);

printf("nnContents of EVEN file:n");
while((a=getw(fp3))!=EOF)
printf("%d",a);

fclose(fp2);
fclose(fp3);
getch();

You might also like