0

I take filenames as arguments of this function and I want to compare two txt files and replace some words in "article". But stack smashing detected error is occured at compiling. What is wrong in this code?

    void fix_spelling_error(char* dictionary, char* article)
    {
    int articleCursor=0, compare, i=0, j, checkSpeller=0, ch;
    char word[100], sub[100];
    FILE *reader, *changer;

    reader = fopen(dictionary, "r");
    changer = fopen(article, "r+");

    if (changer != NULL && reader != NULL)
    {
        while(!feof(changer)
        fscanf(changer, "%s", word);


        while (!feof(reader))
        {
            fscanf(reader, "%s", sub);
            compare = strcmp(word, sub);
            if (compare == -1)
            {
                articleCursor = strlen(word);
                fseek(changer, -articleCursor, SEEK_CUR);
                strcpy(word, sub);
                fprintf(changer, "%s", word);
            }
        }
    fclose(reader);
    fclose(changer);
    }
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
emircand
  • 1
  • 1
  • `while(!feof(changer) fscanf(changer, "%s", word);` won't compile. Please post your actual code as a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). – MikeCAT Apr 25 '21 at 13:26
  • Also your usage of `while (!feof(reader))` is [wrong](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong). – MikeCAT Apr 25 '21 at 13:27
  • so, how can i check end of file condition? – emircand Apr 25 '21 at 13:37
  • Consult the manual for `fscanf` and check what it returns. – Gerhardh Apr 25 '21 at 14:35

0 Answers0