0

I have two arrays which I am reusing. I found that if I don't use memset, the content from the previous loop will stay in the array, messing up my output. The problem now is that when I try to print the content of the array, it won't print a thing, even though the array is not empty. What could it be causing it and how could i fix it? Thanks in advance. `

else{
    while (!feof(fit)){
        fgets(text, MAX, fit);
        xifrar_frase(text, m_clau, text_xifrat);
        fprintf(fit_x, "%s\n", text_xifrat);
        memset(text, '\0', sizeof(text));
        memset(text_xifrat, '\0', sizeof(text));
    }
}

`

I have debugged the code and seen that the mistake is not in the arrays being empty. I am using memset because I found it was the first method that cleared the arrays, if you have a better method I'll happily change it.

  • 1
    You should move the `fgets` call into the loop condition, by changing `while (!feof(fit)){` to `while ( fgets(text, MAX, fit) != NULL )`. Then you probably will not need to use `memset`. – Andreas Wenzel Dec 31 '22 at 20:58
  • @Jan Torres Rodríguez, Why doesn't code check the return value of `fgets()` before using `text`? – chux - Reinstate Monica Dec 31 '22 at 21:02

0 Answers0