0

Here is a code which is giving a user an ability to replace a word in another file but I want to replace a word in the same file. Whenever a user enters a word to replace, the word should be replaced in the same file. How to do this?

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

void findAndReplaceInFile() { 
    FILE *fr, *fw; 
    char word[100], ch, read[100], replace[100];  

    fr = fopen("file.txt", "r"); 
    fw = fopen("new.txt", "w"); 
    if (fr == NULL || fw == NULL) { 
        printf("Can't open file."); 
        exit(0); 
    } 
    printf("Enter the word to find: "); 
    fgets(word, 100, stdin); 
    word[strlen(word) - 1] = word[strlen(word)]; 

    printf("Enter the word to replace it with: "); 
    fgets(replace, 100, stdin); 

    rewind(fr); 
    while (!feof(fr)) { 
        fscanf(fr, "%s", read); 
        if (strcmp(read, word) == 0) { 
            strcpy(read, replace); 
        } 
        fprintf(fw, "%s ", read); 
    } 
    fclose(fr); 
    fclose(fw); 
} 
int main() { findAndReplaceInFile(); } 
Bilal Khan
  • 119
  • 1
  • 9
  • 1
    [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – David Ranieri Jul 06 '20 at 21:03
  • Don't be stingy with your buffers, use 1024 as a default. Secondly, *declare this limit* like `#define BUFFER_SIZE 1024` and replace all instances of that number. This has multiple instances of the "magic number" 100 without explanation. – tadman Jul 06 '20 at 21:05
  • 1
    Please remove the newline from the second user input (you did it with the first input). – Weather Vane Jul 06 '20 at 21:05
  • Is the file small enough you could read the whole thing into memory? – tadman Jul 06 '20 at 21:08
  • 1
    @WeatherVane Thanks for pointing that out. – tadman Jul 06 '20 at 21:09
  • I didn't get you man – Bilal Khan Jul 06 '20 at 21:14
  • 1
    Have you done any search and read the many related posts on Stack Overflow and even on the wider web. [Stack Overflow search results](https://stackoverflow.com/search?q=%5Bc%5D+ranyeplace+word+in+file) – kaylum Jul 06 '20 at 21:14
  • Yes I searched but it was pointing to just reading file but not in the same file – Bilal Khan Jul 06 '20 at 21:26
  • If the file is small you could read it all into memory, do the replacements and then write it out. If the file is too big you could do exactly what you are doing and then delete the old file and rename the new file to the old file's name. – Jerry Jeremiah Jul 06 '20 at 21:27
  • the file is big but I tried to delete the old file and changed its name also but it is not giving me the required result – Bilal Khan Jul 06 '20 at 21:32
  • It is unlikely you can replace the word in the same file unless it is the same length as the old word. If the new word is longer how can you put it in the same file without overwriting something? And if if the word is smaller, what would you do with the extra space in the file? Your approach should work. – Weather Vane Jul 06 '20 at 21:33
  • so is it not possible to replace another word in the same file? – Bilal Khan Jul 06 '20 at 21:55
  • Unless the length of `word` is equal to the length of `replace` -- then it is not possible. Read each word, and write to a new file -- substitution will be fine then. – David C. Rankin Jul 07 '20 at 03:23

1 Answers1

0

I do not think that it is possible to make changes in the file directly, however you can create an impression that you did so! :)

Try to implement file operations:

  1. Suppose you have file.txt (original) and new.txt (replacement file)

  2. Then you can delete file.txt

  3. Rename new.txt to file.txt

alv2017
  • 752
  • 5
  • 14
  • instead of deleting file.txt I will directly delete new.txt but it is still not working. – Bilal Khan Jul 07 '20 at 19:40
  • I thought that you wanted to make replacements in the original file (file. txt) – alv2017 Jul 07 '20 at 19:43
  • Yes I want replacement in the original file but the way you told me is not working. – Bilal Khan Jul 07 '20 at 20:35
  • This is because you directly deleted the file new.txt, and this file contained the changes that you made. As per your code you only read from file.txt, and it doesn't contain any changes. You have to follow the procedure precisely! – alv2017 Jul 07 '20 at 20:40
  • @BilalKhan, I found out that there is a way to edit records directly in the file. The solution would be to use fixed length records and binary files. If you are interested in details, then have a look at chapter 11 "File Processing" of the book "C for Programmers with an Introduction to C11" by Paul Deitel, Harvey Deitel. – alv2017 Jul 09 '20 at 21:58
  • Hey alv, the solution which you provided of deleting and renaming is working but here is a problem that it's not updating means if I try to update something in file, I will have to write not the new updated text to change again but the previous text which I saved earlier in the creation of the file. It is happening like this Bilal change it to John. Again Bilal change it to Mike. It should be like this Bilal change it to John. Then John change it to Mike. – Bilal Khan Jul 13 '20 at 12:17