Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fread and ferror don't set errno

I'm trying to check when fread() raises an error, so I use ferror().

chunk = fread(buf, 1, 100, file);
if (ferror(file))
  {
    return errno;
  }

But, ferror() man page (man 3 ferror, or just man ferror) says:

ERRORS
These functions should not fail and do not set the external variable errno.

So, how can I know the error type occurred when file has been read, although fread() and ferror() didn't set errno?

like image 995
Mahmoud Mubarak Avatar asked Oct 24 '16 09:10

Mahmoud Mubarak


People also ask

Does fread set errno?

fread does not set errno (as you have discovered), and as such you cannot really determine much about a specific error state; only that there is one. The exact nature of the error is generally implementation-dependent.

Does Ferror set errno?

Return Value If no error has occurred on stream, ferror returns 0. Otherwise, it returns a nonzero value. If stream is NULL, ferror invokes the invalid parameter handler, as described in Parameter Validation. If execution is allowed to continue, this function sets errno to EINVAL and returns 0.

Do I need to set errno to 0?

To detect an error, an application must set errno to 0 before calling the function and check whether it is nonzero after the call.

Does Fwrite set errno?

RETURN VALUE If size or nitems is 0, fwrite() returns 0 and the state of the stream remains unchanged. Otherwise, if a write error occurs, the error indicator for the stream is set and errno is set to indicate the error.


2 Answers

You can't get there from here.

fread does not set errno (as you have discovered), and as such you cannot really determine much about a specific error state; only that there is one. The exact nature of the error is generally implementation-dependent. There is no C standard-library-based portable way to gather it .

For specific system-level errors, you can slum it to system-calls, possibly suffering with the pitfalls like poor/nonexistent IO buffering along the way. There POSIX can somewhat come to your rescue. Calls like read, do set errno and have a fairly detailed set of possible outcomes. That may be an option for you if the platform you're working with is POSIX compliant and the code is really so critical to be in-the-know.

But from the C standard library, you're not going to find much beyond being told an error has happened. Generally you'll find you don't need more than that anyway.

like image 165
WhozCraig Avatar answered Oct 24 '22 06:10

WhozCraig


Those functions don't use errno, so you shouldn't either.

It is worth noting that you can tell if everything went smoothly from the return value of fread(). If the return value of fread() differs from the passed nmemb parameter (100 in your case), then you either reached the end of your file or an error occured reading it (source). So test only in that case:

Just drop the use of errno alltogether:

chunk = fread(buf, 1, 100, file);
if (chunk != 100) { // If fread() returns a number different to the nmemb parameter, either error or EOF occured
    if (ferror(file))
      {
        printf("Error occured while reading file.");
        return -1; // Or what ever return value you use to indicate an error
      }
}
like image 34
Magisch Avatar answered Oct 24 '22 06:10

Magisch