首页 > 技术知识 > 正文

首先添加需要得头文件

#include <sys/stat.h> #include <stdio.h> #include <unistd.h> #include <stdlib.h>

第一个是判断文件是否存在,使用acces函数,传入文件得路径

int is_file_exist(const char *file_path) { if(file_path == NULL) return -1; if(access(file_path, F_OK) == 0) return 0; printf(“%s file is not exist!\n”, file_path); return -1; }

根据返回值判断文件是否存在。

然后是设置文件得权限,同样是传入文件得路径,通过chmod函数,更改权限。

int set_file_authotity(const char *file_path) { if(file_path == NULL) return -1; if(chmod(file_path, S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH) !=0 ){ printf(“chmod %s file failed!\n”, file_path); return -1; } return 0; } #define S_IRUSR __S_IREAD /* Read by owner. */ #define S_IWUSR __S_IWRITE /* Write by owner. */ #define S_IXUSR __S_IEXEC /* Execute by owner. */

如上是权限设置得宏

猜你喜欢