Home »
C programs »
C file handling programs
C program to check a specified file has read, write, and execute permission or not
Here, we are going to learn how to check a specified file has read, write, and execute permission or not using C program?
Submitted by Nidhi, on August 13, 2021
Problem statement
Given a file path, we have to check whether a specified file has, read, write, and execute access or not.
C program to check a specified file has read, write, and execute permission or not
The source code to check a specified file has read, write, and execute permission or not is given below. The given program is compiled and executed using GCC compile on UBUNTU 18.04 OS successfully.
// C program to check a specified file has read, write,
// and execute permission or not
#include <stdio.h>
#include <unistd.h>
int main(void)
{
int ret = 0;
ret = access("includehelp.txt", R_OK);
if (ret != -1)
printf("includehelp.txt has read access");
else
printf("includehelp.txt has not read access");
ret = access("includehelp.txt", W_OK);
if (ret != -1)
printf("\nincludehelp.txt has write access");
else
printf("\nincludehelp.txt has not write access");
ret = access("includehelp.txt", X_OK);
if (ret != -1)
printf("\nincludehelp.txt has execute access");
else
printf("\nincludehelp.txt has not execute access");
printf("\n");
return 0;
}
Output
includehelp.txt has read access
includehelp.txt has write access
includehelp.txt has not execute access
Explanation
Here, we checked file "includehelp.txt" has read, write, and execute access using the access() function. Then printed the appropriate message on the console screen.
C File Handling Programs »