[英]How to read a file permission and store it in a variable in C
In Linux platform(Ubuntu system). 在Linux平台(Ubuntu系统)上。 As describe in title.
如标题中所述。 I try to get a number like '0644' and store it in a variable for later use.
我尝试获取一个像“ 0644”的数字并将其存储在变量中以备后用。
the stat, fstat, lstat system calls can be used to retrieve the permission of a file. stat,fstat,lstat系统调用可用于检索文件的权限。
The field st_mode
of the stat structure contains the permission of the file specified as argument of the system call. stat结构的字段
st_mode
包含指定为系统调用参数的文件的权限。 Then a variable of type mode_t
can be used as local storage in your application. 然后,可以将类型为
mode_t
的变量用作应用程序中的本地存储。
This is an example : 这是一个例子:
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#define FILE_NAME "test.c"
int main(int argc, char *argv[])
{
struct stat sb;
mode_t file_permision;
if (stat(FILE_NAME, &sb) == -1) {
perror("stat");
exit(EXIT_FAILURE);
}
file_permision = sb.st_mode;
printf(" File permission : %o (octal)\n",
(unsigned int) file_permision);
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.