How the end of file loop is done

Asked

Viewed 4,312 times

4

Type, I know that the function feof() returns 0 if it did not find a file end and that it requires 1 parameter, which is the pointer_pra_variável_tipo_FILE, so far so good.

If I do the while so it works like the other down but I don’t understand how.

while(!feof(ponteiro_pra_variavel_tipo_FILE)) {
}

It wouldn’t be right to test like this ?

while(feof(ponteiro_pra_variavel_tipo_FILE) == 0){
}

How does he know through !feof(ponteiro_pra_variavel_tipo_FILE), that the function is returning 0 ?

3 answers

4


What happens is that since C does not have the boolean type, it interprets the integer variables to evaluate an expression, being that zero is for false and other than zero is for true, just as we are used to using.
Therefore there is no need to use feof(ponteiro_pra_variavel_tipo_FILE) == 0) because the expression itself already returns an integer value that is sufficient for the evaluation of the while.

For further clarification in your case, the function feof returns 0 when there is still file to be processed, so both expressions cited by you as example have the same meaning. The while will be repeated while the expression is true(the file is not finished).

  • So while the function feof is returning false(0) my while() continues to repeat itself. And when it returns true it ends. This Murilo ?

  • Yes @Guilhermedeoliveira, that’s why you use negation(!) in the case you put of first example. The function feof returns 0 when there is still file to be processed. If the answer served, mark the question as answered.

  • Better explaining, one should be careful when analyzing the expression because the result of the feof 0 does not mean that while will repeat itself while it is false. That is why negation(!) is used in its first example and comparison(==) in the second example.

  • The answer served yes Murilo ! Thank you very much man !

2

The feof function says if any file reading function has ever tried to read some input and failed due to the end of the file. It does not say if a reading function will be able to read the next entry.

What you should do is read the return value of your read functions and see if the read failed due to the end of the file. For example, the functions getchar and scanf return EOF when there are no characters to read. Some examples of this in use:

int c;
while(c = getchar(), c != EOF){
    ...
}

int nr;
int blah;
while(EOF != scanf("%d", &blah)){
    ...
}

Returning to your question, it has to do with the fact that "true or false" is represented with integers in C. Try to make printfs to get an idea:

printf("%d\n", (5 == 5));
printf("%d\n", (5 == 6));
printf("%d\n", !(5 == 6));
printf("%d\n", !3);

And the result is that in C !x and x != 0 are equivalent.

0

As the colleague above put it: C interprets directly, since by default it has this automatic "== 0",

I believe that in broad terms it can be compared to the case of

for (x=0;x<MAX;x++)
   o que executar

instead of

x=o;
while (x<MAX){
execução do código;
x=x+1;
}

All to save a few lines and the code will be thinner and easy to assimilate. At least I see it that way.

Hug

Browser other questions tagged

You are not signed in. Login or sign up in order to post.