Skip to main content
Write a program to copy one file to another. While doing so, all extra spaces in a file should be squeezed to one. For eg. If a file contains line “I am learning C”, it should be converted to “I am learning C”.
CODING:
#include <stdio.h>
#include<conio.h>
void main(int argc,char *argv[])
{
 FILE *fp1,*fp2;
 char ch,word[20]=" ",temp[4]=" ";
 int a=0,b,count=0;
 clrscr();

 if(argc!=3)
 {
printf("Invalid command syntax");
getch();
exit();
 }
 fp1=fopen(argv[1],"r");
 fp2=fopen("word.txt","a");
 strcpy(word,argv[2]);

 if(fp1==NULL)
 {
printf("\nFile not found.");
getch();
exit();
 }

 if(fp2==NULL)
 {
printf("\nError in creating output file 'word.txt'");
getch();
exit();
 }

 while((strcmp("",word)!=0))
 {
while(!feof(fp1))
{
for(b=0;b<3;b++)
{
ch=fgetc(fp1);

if(ch==' '||ch=='\n')
break;
else
temp[a++]=ch;
}
temp[3]='\0';
if(word[0]==temp[0]&&word[1]==temp[1]&&word[2]==temp[2])
count++;
strcpy(temp," ");
a=0;
}
fprintf(fp2,"%s\t- %d\n",word,count);
printf("\nFrequncy of the word '%s' is saved "
"in file 'word.txt'",word);
printf("\n\nEnter another word : ");
gets(word);
count=0;
rewind(fp1);
clrscr();
 }

 fcloseall();
 getch();
}

Comments