0

So the program should let me introduce a word, then search for the word from In.txt then put all the lines where it finds the word in Exit.txt. I have no errors but after I introduce the word I get "Can't read" from the if statement. Any ideas? Thank you very much, I am just getting started with files.

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


int main()
{
FILE* fis, * fis2;
char* sir, * rez, word[50];
printf("Word: ");
gets(word);
sir = malloc(50 * sizeof(char));
fis = fopen("In.txt", "rt");
if (fis == NULL)
    printf("Can't open file!");
else
{
    while (!feof(fis))
    {
        rez = fgets(sir, 50, fis);
        if (rez == NULL)
            printf("Can't read!");
        else
            if (rez == word)
            {
                fis2 = fopen("Exit.txt", "wt");
                fputs(rez, fis2);
            }
    }
}
    fclose(fis);
    free(sir);
    return 0;
}
  • 3
    It should be because it reached at end of file. Note that your usage of `while (!feof(fis))` looks [wrong](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) and that you shouldn't use `gets()`, which has unavoidable risk of buffer overrun, deprecated in C99 and removed from C11. – MikeCAT Aug 09 '20 at 08:59
  • 1
    The problem is of course the comparison `rez == word` which is never true – Antti Haapala -- Слава Україні Aug 09 '20 at 09:13

0 Answers0