Skip to main content
Write a program that counts the frequency of a word from a text file. The program should accept file name as commandline argument. Program should continue to ask wordand display its frequency in a file till the Enter key is pressed without entering any word.
CODING:
#include <stdio.h>
#include<conio.h>
void main()
{
 FILE *fp1,*fp2;
 struct file{
  int cno,bal;
  char actype[8];
 }cdetails[4]={
{101,2000,"Savings"},
{102,5000,"Current"},
{103,3000,"Savings"},
{104,10000,"Current"}
 };
 char ch;
 int a;
 clrscr();

 fp1=fopen("file1.txt","w");
 fp2=fopen("file2.txt","a+");

 if((fp1||fp2)==NULL)
 {
printf("\nError in handling file(s).");
getch();
exit();
 }
 fprintf(fp1,"Customer No.\tAccount Type\tBalance\n");
 fprintf(fp2,"Customer No.\tAccount Type\tBalance\n");

 for(a=0;a<4;a++)
 {
fprintf(fp1,"%d\t\t",cdetails[a].cno);
fprintf(fp1,"%s\t\t",cdetails[a].actype);
fprintf(fp1,"%d\n",cdetails[a].bal);
fprintf(fp2,"%d\t\t",cdetails[a].cno);
fprintf(fp2,"%s\t\t",cdetails[a].actype);
fprintf(fp2,"%d\n",cdetails[a].bal);
 }
 printf("\nData copied to file1.txt successfully.");
 printf("\nContents of file1.txt appended to file2.txt successfully.");
 getch();
 clrscr();
 printf("\nContents of file2.txt :\n\n");
 rewind(fp2);

 while(!feof(fp2))
 {
ch=fgetc(fp2);
printf("%c",ch);
 }

 fcloseall();
 getch();
}

Comments