'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?



Solution 1:[1]

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
      }
}

Solution 2:[2]

This is not a network connection issue, as long as you get an access denied message then your application was able to reach the server

and since you were able to connect using MySQL CLI, try checking passwords for the user as it can be different by connecting client IP

MySQL has two different passwords?

MySQL uses $user/$host/$password comparison by default, so your current setup has two different user accounts in MySQL - root and whatever username you supply on the command line. Each of those has its own password.

Solution 3:[3]

Check your password

Enclose your password within quotes, single or double. For example:

DB_PASSWORD="your_strong_password"

If you password contains any of the quotes used to enclose your password, escape it with a backslash (\). For example:

DB_PASSWORD='your_password\'s_strength'

The answer by @AWS PS is correct, almost. However it does not answer the question #4 raised by asker.

I think the client ec2 IP address shown in the message acts like a client or proxy in order to not disclose the actual RDS server name. Any comment on this would be helpful.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 AWS PS
Solution 3 Arvind K.