I need to figure out how does ftell()
uses errno
to distinguish between success and failure.
I know that the return value of ftell()
would determine the success or failure, but it is possible to return the value of -1 (ftell()
failure) even if on success, and the return is interpreted as a signed value.
So I check the return value of ftell()
and if that is negative, I look at the errno
, and if errno is non-zero, I determine the failure.
However, problem is that errno
can be non-zero even from a previous error(doubtful about this). In this case how do I determine success or failure of ftell()
?
As per Eugene Sh.'s arguments, it seems that it may be unneeded in this particular case, the return value of ftell
should be enough.
Using errno
, in general is done by setting its value to 0
before the function you expect to fail, and is documented as setting the value of errno
.
Later you verify if its value was set, this value represents an implementation defined error code.
§7.21.9.4 - ftell()
Example:
#include <errno.h>
//...
FILE* f = fopen("fil.txt", "r"); // skipping error check
errno = 0;
long res = ftell(f)); // on failure ftell returns -1
if(errno > 0 && res == -1)
{
// error occured
perror("ftell"); // you can use perror to print error message related to the error code
}