二、Linus中的open与close函数

  1.Linus当中重要的工具:

  man手册:在 Linux 系统中,man手册(manual pages)是查询命令、系统调用、库函数等详细文档的核心工具。以下是关于 man 手册的完整使用指南:先简单介绍两个

  man 1是普通的shell命令比如:ls

  man 2是系统调用函数比如:open和close

章节内容类型示例
1用户命令(普通用户可执行的命令)man 1 ls
2系统调用(内核提供的函数)man 2 fork

2.open函数

通过man 2 open操作查询得:

 open函数在Linus系统库的定义(如图):

       #include <sys/types.h>//头文件
       #include <sys/stat.h>
       #include <fcntl.h>

       int open(const char *pathname, int flags);(一般使用用法)
       int open(const char *pathname, int flags, mode_t mode);


  返回值:成功,返回句柄,我们后面对于文件的读写,关闭等都通过句柄来操作。

  失败,返回-1

参数说明: 
  grep -nr "xxxx"./ pathname:文件的路径名,如果只写文件名,就默认当前目录,如果在文件名加上路径,就按照绝对路径来打开文件。 
  flags:表示打开文件后用的操作 底层是一个宏,它可能以十六进制的形式存放。
  O_RDONLY:只读模式 0x 0000 0000 
  O_WRONLY:只写模式 0x 00000001 
  O_RDWR:可读可写 0x 00000002 
  O_APPEND 表示追加,如果原来文件里面有内容,则这次写入会写在文件的最末尾。0x00002000 
  O_CREAT 表示如果指定文件不存在,则创建这个文件 0x0000 0100 
  O_EXCL 表示如果要创建的文件已存在,则出错,同时返回-1,并且修改errno 的值。 
  O_TRUNC 表示截断,如果文件存在,并且以只写、读写方式打开,则将其长度截断为0。             O_NOCTTY 如果路径名指向终端设备,不要把这个设备用作控制终端 。

3.close函数

  通过man 2 open操作查询得:

  在Linux系统库的定义:   

   #include <unistd.h>//头文件

    int close(int fd);

功能:关闭文件;

4.文件权限                                                                                               

  每个文件或目录的权限分为三组:
  所有者(Owner):文件的创建者或拥有者。
  所属组(Group):文件所属的用户组。
  其他用户(Others):系统上的其他用户。
  每组权限包含三种操作:
  读(r):查看文件内容或列出目录内容。
  写(w):修改文件内容或在目录中创建/删除文件。
  执行(x):运行文件(如脚本)或进入目录。
  使用 ls -l 命令查看文件详细信息:
  ls -l 文件名
  输出示例:-rwxr-xr-- 1 user group 4096 Jan 1 12:34 example.tx
  权限符号解析:
  -rwxr-xr-- 分解为:
  -:文件类型(- 表示普通文件,d 表示目录)。
  rwx:所有者权限(读、写、执行)。
  r-x:所属组权限(读、执行,无写权限)。
  r--:其他用户权限(仅读)。                                                                                                        Linux 系统中采用三位十进制数表示权限,如0755, 0644. 
ABCD                                                                                                                                 A- 0, 
  表示十进制 B-用户 C-组用户 D-其他用户   
  ‐‐‐ ‐> 0   (no excute , nowrite ,no read)   
  ‐‐x ‐> 1   excute, (nowrite, no read) 
  ‐w‐ ‐> 2   write    
  r‐‐ ‐> 4   read   

‐wx ‐> 3   write, excute  
r‐x ‐> 5   read, excute   
rw‐ ‐> 6   read, write 

rwx ‐> 7   read, write , excute

5.open和close函数及文件权限的综合应用

vim demo1.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
//int open(const char *pathname, int flags);
//int open(const char *pathname, int flags, mode_t mode);

int main()
{
        int fd;
        fd=open("mm",O_RDONLY|O_CREAT,0755);
        if(fd==-1)
        {
                printf("open file falied");
                perror("why");
                return -1;
        }
        printf("open file success");
        close(fd);
        return 0;
}                      

在demo1.c文件中编写

在生成demo.c文件的可执行文件demo

gcc demo1.c -o demo
./demo

执行后得

ls -l

查看文件的权限属性

文件“mm”满足0755的文件权限。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值