S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP

本文深入探讨了Linux中文件权限的概念,包括S_IRUSR、S_IWUSR等标志的含义,以及如何通过权限设置实现文件的安全管理。同时,介绍了不同类型的文件标识符,如普通文件、目录文件等,并详细解释了它们在系统中的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP

  fd=open("/dev/globalvar", O_RDWR, S_IRUSR|S_IWUSR);     //可读写方式打开设备文件

 

S_IRUSR
Permits the file's owner to read it.

S_IWUSR
Permits the file's owner to write to it.

S_IRGRP
Permits the file's group to read it.

S_IWGRP
Permits the file's group to write to it.

S_ISREG ( ) 普通文件
S_ISDIR ( ) 目录文件
S_ISCHR ( ) 字符特殊文件
S_ISBLK ( ) 块特殊文件
S_ISFIFO ( ) 管道或F I F O
S_ISLNK ( ) 符号连接( P O S I X . 1或S V R 4无此类型)
S_ISSOC K ( ) 套接字(P O S I X . 1或S V R 4无此类型

1.创建文件夹: #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <iostream> using namespace std; int main() { string folder_name = "new_folder"; mkdir(folder_name.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); //创建文件夹 return 0; } 2.复制文件: #include <stdio.h> #include <stdlib.h> int main() { FILE *fp1, *fp2; //定义两个文件指针 char ch; fp1 = fopen("file1.txt", "r"); //打开要复制的文件 fp2 = fopen("file2.txt", "w"); //打开要复制到的文件 while ((ch = fgetc(fp1)) != EOF) { fputc(ch, fp2); //复制文件 } fclose(fp1); fclose(fp2); return 0; } 3.移动文件: #include <stdio.h> #include <stdlib.h> int main() { char old_path[100] = "old_folder/file1.txt"; char new_path[100] = "new_folder/file1.txt"; int result = rename(old_path, new_path); //移动文件 if (result == 0) { printf("移动成功\n"); } else { printf("移动失败\n"); } return 0; } 4.删除文件夹: #include <unistd.h> #include <stdio.h> int main() { char folder_name[100] = "new_folder"; int result = rmdir(folder_name); //删除文件夹 if (result == 0) { printf("删除成功\n"); } else { printf("删除失败\n"); } return 0; } 5.显示文件夹中的内容: #include <dirent.h> #include <stdio.h> int main() { DIR *dir; struct dirent *ent; char folder_name[100] = "new_folder"; dir = opendir(folder_name); //打开文件夹 while ((ent = readdir(dir)) != NULL) { printf("%s\n", ent->d_name); //遍历文件夹中的文件 } closedir(dir); return 0; } 6.查看文件内容: #include <stdio.h> int main() { FILE *fp; char ch; fp = fopen("file1.txt", "r"); //打开文件 while ((ch = fgetc(fp)) != EOF) { printf("%c", ch); //输出文件内容 } fclose(fp); return 0; } 7.修改文件权限: #include <sys/stat.h> #include <stdio.h> int main() { char file_name[100] = "file1.txt"; chmod(file_name, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH); //修改文件权限 return 0; } 8.搜索文件: #include <dirent.h> #include <stdio.h> #include <string.h> int main() { DIR *dir; struct dirent *ent; char folder_name[100] = "new_folder"; char search_name[100] = "file1.txt"; dir = opendir(folder_name); //打开文件夹 while ((ent = readdir(dir)) != NULL) { if (strcmp(ent->d_name, search_name) == 0) //搜索文件 { printf("找到文件:%s\n", ent->d_name); break; } } closedir(dir); return 0; }将上述代码整合成一个完整的程序代码
05-31
头歌操作系统Linux之文件操作[查询结果] 通过man命令可以查询常用的系统调用函数的使用方法。 文件的创建 创建文件的系统调用函数是creat,具体的说明如下: 需要的头文件如下: #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> 函数格式如下: int creat (const char *pathname,mode_t mode); 参数说明: pathname:需要创建文件的绝对路径名或相对路径名; mode:用于指定所创建文件的权限; 常见的mode取值及其含义见下表所示: mode 含义 S_IRUSR 文件所有者的读权限位 S_IWUSR 文件所有者的写权限位 S_IXUSR 文件所有者的执行权限位 S_IRGRP 所有者同组用户的读权限位 S_IWGRP 所有者同组用户的写权限位 S_IXGRP 所有者同组用户的执行权限位 S_IROTH 其他用户的读权限位 S_IWOTH 其他用户的写权限位 S_IXOTH 其他用户的执行权限位 函数返回值说明: 调用成功时,返回值为 文件的描述符(大于0的整数);调用失败时,返回值为-1并设置错误编号errno。 案例演示1: 在当前目录下使用creat函数创建一个名为firstFile的文件,并设置文件的权限为644。详细代码如下所示: #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int main() { int ret = creat("firstFile", S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); if (ret == -1) { printf("创建文件失败\n"); } else { printf("创建文件成功\n"); } return 0; }本关的编程任务是补全右侧代码片段中Begin至End中间的代码,具体要求如下: 在当前目录下创建一个名为testFile的文件,并设置其权限为651。#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> int main() { /********** BEGIN **********/ /********** END **********/ return 0; }
06-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值