Q:

C program to check a specified file has read, write, and execute permission or not

0

C program to check a specified file has read, write, and execute permission or not

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Given a file path, we have to check whether a specified file has, read, write, and execute access or not.

Program:

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.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

File Handling Examples Programs in C language

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C program to create a directory using mkdir() func... >>
<< C program to check a specified file exists or not ...