《Linux C》01 文件I/O之Open操作
2016-04-26 10:59
337 查看
Open函数介绍
Open函数使用实例
Open函数使用实例
Open函数介绍
概要: #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); int creat(const char *pathname, mode_t mode); 描述: 给定一个文件的路径名,open()返回一个负整数的文件描述符,失败返回-1. flags标志中这三个必须包含一个O_RDONLY, O_WRONLY, or O_RDWR. 文件创建标志和文件状态标志的部分列表如下: O_APPEND 在附加模式下打开该文件 O_ASYNC 使能信号驱动I/O,此功能仅可用于终端. O_CLOEXEC 从新的文件描述符使能这个O_CLOEXEC标志 O_NOCTTY 如果pathname指向终端,则不将设备作为此进程的控制终端 O_NDELAY 非阻塞方式操作文件 O_CREAT 如果pathname文件不存在,将被创建。包含这个标志位需为新创建的文件赋予mode,如下:
宏定义 | 宏定义 | 描述 |
---|---|---|
s_irwxu | 00700 | 用户(文件所有者)读,写和执行权限 |
s_irusr | 00400 | 用户有读取权限 |
s_iwusr | 00200 | 用户有写权限 |
s_ixusr | 00100 | 用户的权限 |
s_irwxg | 00070 | 组读,写和执行权限 |
s_irgrp | 00040 | 组有读取权限 |
s_iwgrp | 00020 | 组具有写权限 |
s_ixgrp | 00010 | 组执行权限 |
s_irwxo | 00007 | 所有人具有读、写和执行权限 |
s_iroth | 00004 | 所有人具有读权限 |
s_iwoth | 00002 | 所有人具有写权限 |
s_ixoth | 00001 | 所有人具有执行权限 |
Open函数使用实例
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> int main(){ int fd; fd = open("/home/haha",O_RDWR|O_CREATE,0777);//文件不存在则创建 //fd为-1打开失败,fd为负整数则成功 return 0; }
相关文章推荐
- Linux socket 初步
- Linux Kernel 4.0 RC5 发布!
- linux lsof详解
- linux 文件权限
- Linux 执行数学运算
- 10 篇对初学者和专家都有用的 Linux 命令教程
- 如何组织构建多文件 C 语言程序(二)
- Linux 与 Windows 对UNICODE 的处理方式
- Ubuntu12.04下QQ完美走起啊!走起啊!有木有啊!
- 解決Linux下Android开发真机调试设备不被识别问题
- 运维入门
- 运维提升
- Linux 自检和 SystemTap
- Ubuntu Linux使用体验
- c语言实现hashmap(转载)
- Linux 信号signal处理机制
- linux下mysql添加用户
- Scientific Linux 5.5 图形安装教程