siddharth
siddharth

Reputation: 599

How do I find out if the file is not present or the access rights are not present if the file pointer returns NULL in C?

How do I find out if the file is not present or the access rights are not present if the file pointer returns NULL in C? I am writing code in Linux. And the file has no access, but the file is present so how do I return different status that file was not present or file has no access.

Upvotes: 0

Views: 67

Answers (1)

hmjd
hmjd

Reputation: 121961

Check the value of errno after the attempt to open the file:

if (NULL == (fp = fopen("myfile.txt", "r")))
{
    if (ENOENT != errno)
    {
        fprintf(stderr, "Failed to open file: %s\n", strerror(errno));
    }
    else
    {
        fprintf(stderr, "file does not exist\n");
    }
}

Upvotes: 1

Related Questions