You are on page 1of 9

// WAP to write some text into a file

#include<stdio.h>

#include<string.h>

int main()

char str[100];

FILE *ptr;

ptr=fopen("file1.txt","w");

printf("Enter a Line of text\n");

gets(str);

//puts(str);

fprintf(ptr,"%s",str);

//printf("%s",str);

fclose(ptr);

printf("\nFile created and saved sucessfully...");

return 0;

}
// WAP to read the contents of a file and count no. of vowels and consonant present

in that file

#include<stdio.h>

#include<string.h>

int main()

char c,str[100];

FILE *ptr;

int v=0 , con = 0;

ptr=fopen("file2.txt","w");

printf("Enter a Line of text\n");

gets(str);

fprintf(ptr,"%s",str);

fclose(ptr);

ptr=fopen("file2.txt","r");

while((c= fgetc(ptr))!= EOF)

if(c == ' ' )

continue;

else

if(c == 'a' || c == 'A' || c == 'e' || c == 'E' || c == 'i' || c == 'I' || c == 'o' || c == 'O' || c

== 'u' || c == 'U')

v++;

else

con++;
}

fclose(ptr);

printf("\n Number of Vowels= %d",v );

printf("\n Number of Consonants= %d",con );

return 0;

}
// WAP to store ‘n’ numbers into a file

#include<stdio.h>

#include<string.h>

int main()

int num,I,n;

FILE *ptr;

ptr=fopen("file3.txt","w");

printf("Enter the number to insert\n");

scanf(“%d”,&n);

printf("Enter the numbers \n");

for(i=0;i<n;i++)

scanf("%d",&num);

fprintf(ptr,"%3d",num);

//putw(num,ptr);

fclose(ptr);

return 0;

}
// WAP to store ‘n’ numbers into a file Num.text and count the numbers of even and

odd .

#include<stdio.h>

#include<string.h>

int main()

int num,i,odd=0,even=0,n;

int c;

FILE *ptr;

ptr=fopen("Num.txt","w");

printf("Enter the number to insert\n");

scanf(“%d”,&n);

printf("Enter the numbers \n");

for(i=0;i<n;i++)

scanf("%d",&num);

//fprintf(ptr,"%3d",num);

putw(num,ptr);

fclose(ptr);

ptr=fopen("Num.txt","r");

while((c = getw(ptr))!= EOF)


{

if(c%2== 0)

even++;

else

odd++;

fclose(ptr);

printf("\n Total number of even no.= %d",even);

printf("\n Total number of odd no.= %d",odd);

return 0;

}
// WAP to store ‘n’ numbers into a file Num.text .Read the contents of that file if it is

even then store that in another file named even.txt and other odd.text

#include<stdio.h>

#include<string.h>

int main()

int num,I,n;

int c;

FILE *ptr,*eptr,*optr;

ptr=fopen("Num.txt","w");

printf("Enter the number to insert \n");

scanf(“%d”,&n);

printf("Enter the numbers \n");

for(i=0;i<n;i++)

scanf("%d",&num);

//fprintf(ptr,"%3d",num);

putw(num,ptr);

fclose(ptr);

ptr=fopen("Num.txt","r");

eptr=fopen("Even.txt","w");

optr=fopen("Odd.txt","w");
while((c = getw(ptr))!= EOF)

if(c%2== 0)

putw(c,eptr);

else

putw(c,optr);

fclose(ptr);

fclose(eptr);

fclose(optr);

ptr=fopen("Num.txt","r");

printf("\n content of Main file is :");

while((c = getw(ptr))!= EOF)

printf("\t%d",c);

fclose(ptr);

ptr=fopen("Odd.txt","r");

printf("\n content of Odd file is :");

while((c = getw(ptr))!= EOF)

printf("\t%d",c);
}

fclose(ptr);

ptr=fopen("Even.txt","r");

printf("\n content of Even file is :");

while((c = getw(ptr))!= EOF)

printf("\t%d",c);

fclose(ptr);

return 0;

You might also like