Check if File has Write Permission in C
To check if a file has write permission in C, you can use several approaches such as using the access()
function, the stat()
function, or by attempting to open the file in write mode using fopen()
.
Example 1: Check Write Permission using access()
In this example, we will use the access()
function from <unistd.h>
with the W_OK
flag to check if a file has write permission.
main.c
#include <stdio.h>
#include <unistd.h>
int main() {
const char *filename = "example.txt";
// Check if file has write permission using access()
if (access(filename, W_OK) == 0) {
printf("File '%s' has write permission.\n", filename);
} else {
printf("File '%s' does not have write permission.\n", filename);
}
return 0;
}
Explanation:
- We include
<unistd.h>
to use theaccess()
function and<stdio.h>
for input/output operations. - The constant pointer
filename
stores the name of the file to check. - The
access()
function is called with the file name andW_OK
flag. It returns 0 if the file is writable. - An
if
statement checks the return value ofaccess()
to print whether the file has write permission or not.
Output:
File 'example.txt' has write permission.
Example 2: Check Write Permission using stat()
In this example, we will use the stat()
function from <sys/stat.h>
to retrieve file metadata and then check the permission bits to see if the file is writable by its owner.
main.c
#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) == -1) {
perror("stat");
return 1;
}
// Check if the file is writable by the owner using permission bits
if (fileStat.st_mode & S_IWUSR) {
printf("File '%s' is writable by the owner.\n", filename);
} else {
printf("File '%s' is not writable by the owner.\n", filename);
}
return 0;
}
Explanation:
- We include
<sys/stat.h>
for thestat()
function and<stdio.h>
for I/O operations. - The
filename
constant holds the name of the file, and astruct stat
variablefileStat
is declared to store file information. - The
stat()
function fills thefileStat
structure with the file’s metadata; if it fails, an error is reported usingperror()
. - The bitwise AND operation checks if the
S_IWUSR
flag is set infileStat.st_mode
, indicating the file is writable by the owner.
Output:
File 'example.txt' is writable by the owner.
Example 3: Check Write Permission by Attempting to Open File
In this example, we will try opening the file in append mode using fopen()
. Successfully opening the file indicates that it has write permission.
main.c
#include <stdio.h>
int main() {
const char *filename = "example.txt";
// Attempt to open the file in append mode to check write permission
FILE *file = fopen(filename, "a");
if (file != NULL) {
printf("File '%s' can be opened for writing.\n", filename);
fclose(file);
} else {
printf("File '%s' cannot be opened for writing.\n", filename);
}
return 0;
}
Explanation:
- We include
<stdio.h>
for file input/output operations. - The constant
filename
holds the name of the file to check. - The
fopen()
function is used with the mode"a"
(append) which requires write permission. If the file opens successfully, it indicates write access. - An
if
statement checks if the file pointer is notNULL
; if true, the file is writable, otherwise it is not.
Output:
File 'example.txt' can be opened for writing.
Conclusion
In this tutorial, we explored multiple methods to check if a file has write permission in C. We learned how to use the access()
function for a straightforward check, the stat()
function to inspect file permission bits, and fopen()
to verify writability by attempting to open the file in append mode.