unix/linux文件操作函数open的几种flag

int open(const char *pathname, int flags, mode_t mode)

pathname:文件名(文件路径) 

mode:使用权限(同Linux文件权限相似4-r,w-2,x-1)

flags:以某种模式打开文件

flags具体参数:

       O_RDONLY :只读权限

        O_WRONLY:只写权限

        O_RDWR:读写权限

        O_CREAT:若文件不存在将创建一个新文件

        O_EXCL:通过 O_CREAT, 生成 文件,若文件已经 存在,则open出错,调用失败

        O_TRUNC:文件已经存在,且是一个普通文件 ,打开 模式又是 可写 , 清空文件内容(清空原 有内容,重写)

        O_APPEND:文件以追加模式打开,在写以前 ,文件读写指针被置在文件的末尾(追加模文件内容)

返回值

        open函数执行成功返回一个新的文件描述符 (若是有错误 发生返回-1,并在errno设置错误信息)

demo1:

1.创建file文件

2.修改文件权限

3.测试O_EXCL

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>

void result()
{
        umask(0);
        int fd;
        fd = open("/root/pro/file",O_CREAT|O_EXCL,0777);
        if(fd == -1)
        {
                printf("file exist\n");
        }
        close(fd);
}
int main()
{
        result();
        return 0;
}

结果演示

 demo2:

1.创建文件(文件内容:hello)

2.修改文件权限

3.测试O_APPEND(原有文件内容后面追加word)

4.测试O_TRUNC(依据上步,修改文件内容为hello)

结果演示

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>

void result()
{
        int fd;
        int fd_write;
        char *write_buf = "word\n";
        fd = open("/root/pro/file",O_APPEND|O_RDWR,0777);
        if(fd == -1)
        {
                printf("open file fail\n");
                perror("why");
                exit(-1);
        }
        fd_write = write(fd,write_buf,strlen(write_buf));
        if(fd_write == -1)
        {
                printf("write file fail\n");
                perror("why");
                exit(-1);
        }
        close(fd);
}
int main()
{
        result();
        return 0;
}

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>

void result()
{
        int fd;
        int fd_write;
        char *write_buf = "hello\n";
        fd = open("/root/pro/file",O_TRUNC|O_RDWR,0777);
        if(fd == -1)
        {
                printf("open file fail\n");
                perror("why");
                exit(-1);
        }
        fd_write = write(fd,write_buf,strlen(write_buf));
        if(fd_write == -1)
        {
                printf("write file fail\n");
                perror("why");
                exit(-1);
        }
        close(fd);
}
int main()
{
        result();
        return 0;
}

### Python `open` 函数Flag 参数说明 在 Python 中,`open()` 函数用于打开文件并返回一个对应的文件对象。虽然标准库文档中并未直接提及名为 “flag”的参数[^1],但在某些操作系统接口或高级用法场景下确实存在类似的标志位概念。 对于更底层的操作系统级控制,在 Unix 或类 Unix 系统上可以使用内置模块 `_io` 下较低层次的方法来实现带有特定 flags 的文件操作;然而这超出了常规 `open()` 方法的应用范围。通常情况下,Python 用户会通过指定模式字符串(即第二个参数 `mode`)间接设置这些行为: - `'r'`: 只读方式打开,默认值。 - `'w'`: 写入方式打开,如果文件已存在则覆盖其内容。 - `'a'`: 追加写入方式打开,所有新数据都将被追加到现有内容之后。 - `'+'`: 更新方式打开,允许同时读取和写入。 - `'b'`: 二进制模式处理文件。 - `'t'`: 文本模式处理文件(默认)。 当涉及到更加细致化的权限设定或是特殊类型的文件访问需求时,则可能需要用到额外的功能扩展包如 `os.open()`, 它接受 POSIX 风格的整数型标记作为第三个可选参数以提供更多选项支持。 #### 使用 os.open 设置 Flags 示例 ```python import os flags = os.O_CREAT | os.O_EXCL | os.O_WRONLY # 创建|独占锁|只写 file_descriptor = os.open('example.txt', flags) with open(file_descriptor, 'wt') as f: f.write("Hello world!") os.close(file_descriptor) ``` 上述代码片段展示了如何利用 `os.open()` 来创建具有自定义标志的新文件描述符,并将其传递给普通的 `open()` 调用来完成进一步的工作流集成。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值