
#include
int access(const char *pathname, int mode);
作用:判断某个文件是否有某个权限,或者判断文件是否存在
参数:
-pathname:判断的文件路径
-mode:
R_OK:判断是否有读权限
W_OK
X_OK
F_OK:判断文件是否存在
返回值:
成功返回0,失败返回-1.
- #include
- #include
- int main() {
- int ret = access("a.txt", F_OK);
- if(ret == -1) {
- perror("access");
- return -1;
- }
- printf("文件存在!!!\n");
- return 0;
- }
#include
int chmod(const char *pathname, mode_t mode);
作用:修改文件的权限
参数:
-pathname:修改文件的路径
-mode:需要修改的权限值,八进制的数
返回值:成功返回0,失败返回-1
- #include
- #include
- int main() {
- int ret = chmod("a.txt", 0775);
- if(ret == -1) {
- perror("chmod");
- return -1;
- }
- return 0;
- }
#include
#include
int truncate(const char *path, off_t length);
作用:缩减或扩展文件的尺寸至指定的大小
参数:
-path:需要修改的文件的路径
-length:需要最终文件变成的大小
返回值:成功返回0,失败返回-1
- #include
- #include
- #include
- int main() {
- int ret = truncate("b.txt", 20);
- if(ret == -1) {
- perror("truncate");
- return -1;
- }
- return 0;
- }