Replace a word in a file
Find a specific word from an existing file and replace that word with another.
Code
/******Word replace in a file******/ #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { FILE *fp,*fp1; char str[15],str1[15],fname[20],ch; int i=0,len,flag,x=0; //Creating a file printf("\nEnter a file name:"); gets(fname); fp=fopen(fname,"w"); if(fp==NULL){ printf("\nFile cannot be created."); exit(0); } if(ferror(fp)!=0){ printf("\nFile become damage or corrupt."); exit(0); } //Enter the content in the file printf("\nEnter the content of the file. press ctrl+z after compulsion.\n\n"); while((ch=getchar())!=EOF) putc(ch,fp); fclose(fp); //Open the file in read mode fp=fopen(fname,"r"); if(fp==NULL){ printf("\nFile cannot be created."); exit(0); } if(ferror(fp)!=0){ printf("\nFile become damage or corrupt."); exit(0); } //Print the content of the file printf("\nThe content of the current file is:\n"); while((ch=getc(fp))!=EOF) printf("%c",ch); fclose(fp); //Open the file in read mode fp=fopen(fname,"r"); if(fp==NULL){ printf("\nFile cannot be created."); exit(0); } if(ferror(fp)!=0){ printf("\nFile become damage or corrupt."); exit(0); } //Taking input of the word to be replace printf("\nEnter the word you want to replace: "); scanf("%s",str); len=strlen(str); printf("\nEnter replaced word: "); scanf("%s",str1); //Open a new file to copy all the content of the old file with the replace word fp1=fopen("new.txt","w"); if(fp==NULL){ printf("\nFile cannot be created."); exit(0); } if(ferror(fp)!=0){ printf("\nFile become damage or corrupt."); exit(0); } while((ch=getc(fp))!=EOF){ if((ch!=32) && (ch!='\n')){ flag=0; i=0; do{ if((ch==str[i]) && (i<len)) i++; else{ flag=1; break; } }while(((ch=getc(fp))!=EOF) && (ch!=32) && (ch!='.') && (ch!='\n') && (ch!='?') && (ch!='!')); if(flag==0){ fprintf(fp1,"%s",str1); if(ch==32) putc(ch,fp1); if(ch=='\n') putc(ch,fp1); if(ch=='?') fprintf(fp1,"%c ",ch); if(ch=='!') fprintf(fp1,"%c ",ch); if(ch=='.') fprintf(fp1,"%c ",ch); x++; } else{ if(i!=0) fseek(fp,-(i+1),SEEK_CUR); do{ putc(ch,fp1); }while(((ch=getc(fp))!=EOF) && (ch!=32) && (ch!='.') && (ch!='\n') && (ch!='?') && (ch!='!')); if(ch==32) putc(ch,fp1); if(ch=='\n') putc(ch,fp1); if(ch=='?') fprintf(fp1,"%c ",ch); if(ch=='!') fprintf(fp1,"%c ",ch); if(ch=='.') fprintf(fp1,"%c ",ch); } } } printf("\n%d word(s) are found and replace successfully.",x); fclose(fp); fclose(fp1); fp1=fopen("new.txt","r"); printf("\nThe content of the new file is:\n"); while((ch=getc(fp1))!=EOF) printf("%c",ch); fclose(fp1); return 0; }
Output
Enter a file name:temp.txt Enter the content of the file. press ctrl+z after compulsion. This is a temp file. This file is for testing. ^Z The content of the current file is: This is a temp file. This file is for testing. Enter the word you want to replace: file Enter replaced word: program 2 word(s) are found and replace successfully. The content of the new file is: This is a temp program. This program is for testing.