Check if File Has Read Permission in C

To check if a file has read permission in C, you can use various approaches such as the access() function, fopen() function, or the stat() system call.


Example 1: Using access() Function

In this example, we will use the access() function from unistd.h to check if a file has read permission. The access() function returns 0 if the file is accessible with the specified mode, and -1 otherwise.

main.c

</>
Copy
#include <stdio.h>
#include <unistd.h>

int main() {
    const char *filename = "example.txt";
    
    // Check if the file has read permission using access()
    if (access(filename, R_OK) == 0) {
        printf("File '%s' has read permission.\n", filename);
    } else {
        printf("File '%s' does not have read permission or does not exist.\n", filename);
    }
    
    return 0;
}

Explanation:

  1. The variable filename holds the name of the file we want to check.
  2. The access() function is called with R_OK to check for read permission.
  3. If access() returns 0, it indicates that the file is accessible for reading, and a success message is printed using printf().
  4. If access() returns -1, it means the file does not have read permission or may not exist, and an error message is printed.

Output:

File 'example.txt' has read permission.

Example 2: Using fopen() Function

In this example, we will attempt to open the file in read mode using the fopen() function. A successful open indicates that the file has read permission.

main.c

</>
Copy
#include <stdio.h>

int main() {
    const char *filename = "example.txt";
    
    // Try to open the file for reading using fopen()
    FILE *file = fopen(filename, "r");
    if (file != NULL) {
        printf("File '%s' can be read.\n", filename);
        fclose(file);
    } else {
        printf("File '%s' cannot be read or does not exist.\n", filename);
    }
    
    return 0;
}

Explanation:

  1. The variable filename contains the name of the file to check.
  2. The fopen() function is used with the mode "r" to attempt opening the file for reading.
  3. If fopen() returns a non-NULL pointer, it means the file opened successfully and has read permission; the file is then closed using fclose().
  4. If fopen() returns NULL, it indicates that the file cannot be read or does not exist, and an error message is printed.

Output:

File 'example.txt' can be read.

Example 3: Using stat() Function

In this example, we will use the stat() function to retrieve the file’s status and then check the permission bits to see if the file has read permission for the owner.

main.c

</>
Copy
#include <stdio.h>
#include <sys/stat.h>

int main() {
    const char *filename = "example.txt";
    struct stat fileStat;
    
    // Retrieve file status using stat()
    if (stat(filename, &fileStat) < 0) {
        printf("Unable to get file status for '%s'.\n", filename);
        return 1;
    }
    
    // Check if the owner has read permission using the S_IRUSR flag
    if (fileStat.st_mode & S_IRUSR) {
        printf("File '%s' has read permission for the owner.\n", filename);
    } else {
        printf("File '%s' does not have read permission for the owner.\n", filename);
    }
    
    return 0;
}

Explanation:

  1. The variable filename stores the name of the file to be checked.
  2. The stat() function is called to fill the fileStat structure with the file’s status information.
  3. The expression fileStat.st_mode & S_IRUSR checks if the read permission bit for the owner (S_IRUSR) is set.
  4. If the condition is true, it means the owner has read permission; otherwise, the file does not have the read permission for the owner.

Output:

File 'example.txt' has read permission for the owner.

Conclusion

In this tutorial, we explored three different approaches to check if a file has read permission in C:

  1. access() Function: Quickly checks file accessibility using R_OK.
  2. fopen() Function: Attempts to open the file in read mode and determines permission based on success.
  3. stat() Function: Retrieves file status and inspects the permission bits for the owner.