1

I want to find a way to replace the word that was found the same with the user input. For instance, the user inputs "er" and then these words come up:

enter image description here

What I want to do is replace the "er" on all of these words with again a user input. for example , the user then writes "el" instead of "er". When this happens, the original file will not change but another file will be created that will save all the changes made. Does anyone have an idea?

#include <stdlib.h>
#include <string.h>
#include<conio.h>
#define MAX 1024

int main(int argc, char* argv[]) {

    FILE* myFile = fopen("C:\\Users\\doom\\Desktop\\txtfiles\\singlewords.txt", "r+");
    FILE* myFile1 = fopen("C:\\Users\\doom\\Desktop\\txtfiles\\sentences.txt", "r+");
    FILE* tempFile = fopen("C:\\Users\\doom\\Desktop\\txtfiles\\singlewordstemp.txt", "w");
    char inputWord[MAX];
    char inputRepl[MAX];
    char lineBuffer[MAX];
    char all_lines[MAX];
    //char buffer[MAX + 2];
    //char* buff_ptr, * find_ptr;
    //size_t find_len = strlen(lineBuffer);

    if (myFile == NULL)
    {
        printf("File Does Not Exist \n");
        return 1;
    }
    strcat(all_lines, lineBuffer);
    printf("Enter the word \n");
    fgets(inputWord, MAX, stdin);
    //printf("\nEnter String to replace:");
    //scanf("%s", inputRepl);
    while (!feof(myFile))
    {
        char lineBuffer[1024];
        fscanf(myFile, "%1024[^\n]\n", lineBuffer);
        //printf("%s\n", lineBuffer);
        while (fgets(lineBuffer, MAX, myFile)) {
            if (strstr( lineBuffer, inputWord))
                puts(lineBuffer);

        }
    }
}
ImSkilzBTW
  • 11
  • 3
  • Does this answer your question? [What is the function to replace string in C?](https://stackoverflow.com/questions/779875/what-is-the-function-to-replace-string-in-c) – Robert Harvey Feb 29 '20 at 18:13
  • I tried it before, but I am struggling because I want to change the words inside the text file regarding the user input. I am having trouble thinking about replacing all the words found with the user input. I don't know how to alter the words all at once. L – ImSkilzBTW Feb 29 '20 at 18:19
  • [This answer](https://stackoverflow.com/a/779960/102937) appears to already have that capability. Study the code. – Robert Harvey Feb 29 '20 at 18:28
  • regarding: `FILE* myFile = fopen("C:\\Users\\doom\\Desktop\\txtfiles\\singlewords.txt", "r+"); FILE* myFile1 = fopen("C:\\Users\\doom\\Desktop\\txtfiles\\sentences.txt", "r+"); FILE* tempFile = fopen("C:\\Users\\doom\\Desktop\\txtfiles\\singlewordstemp.txt", "w")` 1) The question says that the original files are not to be modified, so why the mode: `r+` rather than just `r`? (cont) – user3629249 Feb 29 '20 at 19:58
  • (cont) 2) always check for success immediately, via something similar to: `if( ! myFile ) { perror( "fopen to read file1 failed" );` Then the correct error text will be output to `stderr`, before the contents of `errno` are modified – user3629249 Feb 29 '20 at 19:58
  • why [while (!feof(myFile)) is always wrong](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – user3629249 Feb 29 '20 at 20:02
  • regarding: `fscanf(myFile, "%1024[^\n]\n", lineBuffer);` That final `\n` is seen as `white space` and will create the problem that the call to `fscanf()` will not return until some non-white space character is encountered. This will create problems at run time. Suggest: `fscanf(myFile, " %1024[^\n]", lineBuffer);` Notice the leading space in the format string. – user3629249 Feb 29 '20 at 20:07
  • regarding the first occurrence of : `strcat(all_lines, lineBuffer);` the buffer: `lineBuffer` has not been initialized, And the buffer `all_lines` has not been initialized. The result is undefined behavior. Suggest removing the statement: `strcat(all_lines, lineBuffer);` AND replacing: `char all_lines[MAX];` with: `char all_lines[MAX] = {'\0'};` – user3629249 Feb 29 '20 at 20:10
  • regarding: `char lineBuffer[MAX];` and `fscanf(myFile, "%1024[^\n]\n", lineBuffer);` The MAX CHARACTERS modifier must be 1 less than the length of the input buffer because the 'input format conversion' specifiers `%s` and `%[...]` always append a NUL byte to the input. This avoids any possibility of a buffer overrun and the resulting undefined behavior – user3629249 Feb 29 '20 at 20:14
  • regarding: `fgets(inputWord, MAX, stdin); .... while (!feof(myFile))` Strongly suggest: `while( fgets(inputWord, MAX, stdin) )` which uses the returned value from `fgets()` to determine if the end of the file has been reached – user3629249 Feb 29 '20 at 20:16
  • OT: regarding: `#include` 1) this is not a portable header file, so should not be used. 2) none of the items within that header file are used in the posted code. Header files those contents are not used should not be included – user3629249 Feb 29 '20 at 20:23
  • Please do not post screen shots, Rather, copy/paste the text into your question – user3629249 Feb 29 '20 at 20:26

0 Answers0