Linux系统编程 40 open函数

学习笔记

函数原型:

int open(const char *pathname, init flags);
int open(const char *pathname, int flags,mode_t mode);
int close(int fd);

open 成功的时候,会返回一个整数
          失败的时候,会返回-1
      
pathname : 文件路径
flags         : 读写权限
mode        : 用户 用户组 其他用户的权限 八进制形式

权限同时受umask的影响 
文件权限=mode&~umask

两个版本的区别:
第一个open用于打开已经存在的文件
第二个open用于打开不存在的文件

常用的参数

O_RDONLY 
O_WRONLY
O_RDWR

O_APPEND   防止清空,在末尾添加
O_CREAT
O_EXCL        是否存在
O_TRUNC    把文件截断成0.就是把文件清零
      
O_NONBLOCK  不阻塞

先创建文件

$touch dict.txt

小细节:

#include <sys/types.h>
 #include <sys/stat.h>

这两个头文件可以用#include<unistd.h> //#unix stdandard 来替换

$cat open.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<pthread.h>
#include<unistd.h> //#unix stdandard
int main(int argc, char *argv[])
{int fd;fd = open("./dict.txt",O_RDONLY);printf("fd = %d\n",fd);close(fd);return 0;
}$gcc open.c -o open
open.c: In function ‘main’:
open.c:10:25: error: ‘O_RDONLY’ undeclared (first use in this function)fd = open("./dict.txt",O_RDONLY);^
open.c:10:25: note: each undeclared identifier is reported only once for each function it appears in

添加头文件

#include<fcontl.h>
$gcc open.c -o open
$./open
fd = 3

打开一个不存在的文件
修改代码如下:

$cat open.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<pthread.h>
#include<unistd.h> //#unix stdandard #include<fcntl.h>
int main(int argc, char *argv[])
{int fd;//fd = open("./dict.txt",O_RDONLY);fd = open("./dict.cpp",O_RDONLY | O_CREAT,0644);//rw -r-r-printf("fd = %d\n",fd);close(fd);return 0;
}

可以看到dict.cpp的权限和设计意图一样。

$gcc open.c -o open
$ls
dict.txt  open  open.c
$./open
fd = 3
$ls
dict.cpp  dict.txt  open  open.c
$ll
total 24
drwxrwxr-x  2 ubuntu ubuntu 4096 Dec 13 22:36 ./
drwxrwxrwx 10 root   root   4096 Dec 13 22:16 ../
-rw-r--r--  1 ubuntu ubuntu    0 Dec 13 22:36 dict.cpp
-rw-rw-r--  1 ubuntu ubuntu    0 Dec 13 22:23 dict.txt
-rwxrwxr-x  1 ubuntu ubuntu 8621 Dec 13 22:36 open*
-rw-rw-r--  1 ubuntu ubuntu  860 Dec 13 22:35 open.c

O_TRUNC 用于清空文件内容

现在在dict.cpp文件中添加内容

$cat dict.cpp
sfajfljlfjasdflas
eerqewre
sad
f
asdfdfasdfadsg
as
fs
df
ads
fsad
f
as
gagadsaghfjk
$vi dict.cpp
$ll
total 28
drwxrwxr-x  2 ubuntu ubuntu 4096 Dec 14 08:58 ./
drwxrwxrwx 10 root   root   4096 Dec 13 22:16 ../
-rw-r--r--  1 ubuntu ubuntu  598 Dec 14 08:58 dict.cpp
-rw-rw-r--  1 ubuntu ubuntu    0 Dec 13 22:23 dict.txt
-rwxrwxr-x  1 ubuntu ubuntu 8621 Dec 13 22:36 open*
-rw-rw-r--  1 ubuntu ubuntu  934 Dec 14 08:56 open.c

修改程序 open.c

$vi open.c
$cat open.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<pthread.h>
#include<unistd.h> //#unix stdandard #include<fcntl.h>
int main(int argc, char *argv[])
{int fd;//fd = open("./dict.txt",O_RDONLY);//fd = open("./dict.cpp",O_RDONLY | O_CREAT,0644);//rw -r-r-fd = open("./dict.cpp",O_RDONLY | O_CREAT| O_TRUNC,0644);//rw -r-r-printf("fd = %d\n",fd);close(fd);return 0;
}

fd = open("./dict.cpp",O_RDONLY | O_CREAT| O_TRUNC,0644);//rw -r-r-
这个函数的意思是
1.如果文件不存在,这以指定的权限创建一个文件
2.如果文件存在,以只读方式打开,并截断成0.则该文件清空

$gcc open.c -o open
l$ls
dict.cpp  dict.txt  open  open.c
$./open
fd = 3
$ll
total 24
drwxrwxr-x  2 ubuntu ubuntu 4096 Dec 14 09:01 ./
drwxrwxrwx 10 root   root   4096 Dec 13 22:16 ../
-rw-r--r--  1 ubuntu ubuntu    0 Dec 14 09:01 dict.cpp
-rw-rw-r--  1 ubuntu ubuntu    0 Dec 13 22:23 dict.txt
-rwxrwxr-x  1 ubuntu ubuntu 8621 Dec 14 09:01 open*
-rw-rw-r--  1 ubuntu ubuntu  928 Dec 14 09:00 open.c

文件被清空(没有内容)

$cat dict.cpp
$

查看文件权限

$umask
0002

修改程序

$cat open.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<pthread.h>
#include<unistd.h> //#unix stdandard #include<fcntl.h>
int main(int argc, char *argv[])
{int fd;//fd = open("./dict.txt",O_RDONLY);//fd = open("./dict.cpp",O_RDONLY | O_CREAT,0644);//rw -r--r--//    fd = open("./dict.cpp",O_RDONLY | O_CREAT| O_TRUNC,0644);//rw -r--r--fd = open("./dict.cp2",O_RDONLY | O_CREAT| O_TRUNC,0777);//rwxrwxrwxprintf("fd = %d\n",fd);close(fd);return 0;
}
$gcc open.c -o open
$ls
dict.cpp  dict.txt  open  open.c
$./open
fd = 3
$ls
dict.cp2  dict.cpp  dict.txt  open  open.c
$ll
total 24
drwxrwxr-x  2 ubuntu ubuntu 4096 Dec 14 12:43 ./
drwxrwxrwx 10 root   root   4096 Dec 13 22:16 ../
-rwxrwxr-x  1 ubuntu ubuntu    0 Dec 14 12:43 dict.cp2*
-rw-r--r--  1 ubuntu ubuntu    0 Dec 14 09:01 dict.cpp
-rw-rw-r--  1 ubuntu ubuntu    0 Dec 13 22:23 dict.txt
-rwxrwxr-x  1 ubuntu ubuntu 8621 Dec 14 12:43 open*
-rw-rw-r--  1 ubuntu ubuntu 1007 Dec 14 12:42 open.c

777&(~002)=775  rwxrwx-wx 符合设计意图

open常见的错误

1.打开的文件不存在,返回值为-1

$cat open.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<pthread.h>
#include<unistd.h> //#unix stdandard
#include<errno.h>
#include<fcntl.h>
int main(int argc, char *argv[])
{int fd;//fd = open("./dict.txt",O_RDONLY);//fd = open("./dict.cpp",O_RDONLY | O_CREAT,0644);//rw -r-r-//    fd = open("./dict.cpp",O_RDONLY | O_CREAT| O_TRUNC,0644);//rw -r-r-//fd = open("./dict.cp2",O_RDONLY | O_CREAT| O_TRUNC,0777);//rwx -rwx-wxr-fd = open("./dict.cp4",O_RDONLY);printf("fd = %d errno=%d\n",fd,errno);close(fd);return 0;
}
$gcc open.c -o open
$./open
fd = -1 errno=2

errno =2的含义可以通过
char *strerror(int errnum);(要加string.h)翻译出来

$cat open.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<pthread.h>
#include<unistd.h> //#unix stdandard
#include<errno.h>
#include<fcntl.h>
int main(int argc, char *argv[])
{int fd;//fd = open("./dict.txt",O_RDONLY);//fd = open("./dict.cpp",O_RDONLY | O_CREAT,0644);//rw -r-r-//    fd = open("./dict.cpp",O_RDONLY | O_CREAT| O_TRUNC,0644);//rw -r-r-//fd = open("./dict.cp2",O_RDONLY | O_CREAT| O_TRUNC,0777);//rwx -rwx-wxr-fd = open("./dict.cp4",O_RDONLY);printf("fd = %d errno=%d %s \n",fd,errno,strerror(errno));close(fd);return 0;
}
$gcc open.c -o open
$./open
fd = -1 errno=2 No such file or directory 

2.以写方式打开只读文件(打开文件没有对应的权限)

$touch dict.cp3
$ll
total 24
drwxrwxr-x  2 ubuntu ubuntu 4096 Dec 14 12:59 ./
drwxrwxrwx 10 root   root   4096 Dec 13 22:16 ../
-rwxrwxr-x  1 ubuntu ubuntu    0 Dec 14 12:43 dict.cp2*
-rw-rw-r--  1 ubuntu ubuntu    0 Dec 14 12:59 dict.cp3
-rw-r--r--  1 ubuntu ubuntu    0 Dec 14 09:01 dict.cpp
-rw-rw-r--  1 ubuntu ubuntu    0 Dec 13 22:23 dict.txt
-rwxrwxr-x  1 ubuntu ubuntu 8737 Dec 14 12:57 open*
-rw-rw-r--  1 ubuntu ubuntu 1102 Dec 14 12:56 open.c
$chmod u-w dict.cp3
$ll
total 24
drwxrwxr-x  2 ubuntu ubuntu 4096 Dec 14 12:59 ./
drwxrwxrwx 10 root   root   4096 Dec 13 22:16 ../
-rwxrwxr-x  1 ubuntu ubuntu    0 Dec 14 12:43 dict.cp2*
-r--rw-r--  1 ubuntu ubuntu    0 Dec 14 12:59 dict.cp3
-rw-r--r--  1 ubuntu ubuntu    0 Dec 14 09:01 dict.cpp
-rw-rw-r--  1 ubuntu ubuntu    0 Dec 13 22:23 dict.txt
-rwxrwxr-x  1 ubuntu ubuntu 8737 Dec 14 12:57 open*
-rw-rw-r--  1 ubuntu ubuntu 1102 Dec 14 12:56 open.c
$vim open.c
$cat open.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<pthread.h>
#include<unistd.h> //#unix stdandard
#include<errno.h>
#include<fcntl.h>
int main(int argc, char *argv[])
{int fd;//fd = open("./dict.txt",O_RDONLY);//fd = open("./dict.cpp",O_RDONLY | O_CREAT,0644);//rw -r-r-//    fd = open("./dict.cpp",O_RDONLY | O_CREAT| O_TRUNC,0644);//rw -r-r-//fd = open("./dict.cp2",O_RDONLY | O_CREAT| O_TRUNC,0777);//rwx -rwx-wxr-//fd = open("./dict.cp4",O_RDONLY);fd = open("./dict.cp3",O_WRONLY);printf("fd = %d errno=%d %s \n",fd,errno,strerror(errno));close(fd);return 0;
}
$gcc open.c -o open
$./open
fd = -1 errno=13 Permission denied
$

3.以只写方式打开目录

程序为

$cat open.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<pthread.h>
#include<unistd.h> //#unix stdandard
#include<errno.h>
#include<fcntl.h>
int main(int argc, char *argv[])
{int fd;//fd = open("./dict.txt",O_RDONLY);//fd = open("./dict.cpp",O_RDONLY | O_CREAT,0644);//rw -r-r-//    fd = open("./dict.cpp",O_RDONLY | O_CREAT| O_TRUNC,0644);//rw -r-r-//fd = open("./dict.cp2",O_RDONLY | O_CREAT| O_TRUNC,0777);//rwx -rwx-wxr-//fd = open("./dict.cp4",O_RDONLY);//fd = open("./dict.cp3",O_WRONLY);fd = open("./mydir",O_WRONLY);printf("fd = %d errno=%d %s \n",fd,errno,strerror(errno));close(fd);return 0;
}
$mkdir mydir
$vim open.c
$gcc open.c -o open
$./open
fd = -1 errno=21 Is a directory 

Linux系统编程 40 -open函数相关推荐

  1. 【Linux系统编程】vfork() 函数详解

    00. 目录 文章目录 00. 目录 01. vfork函数 02. fork和vfork区别 03. 父子进程地址空间 04. 附录 01. vfork函数 函数分析 #include <sy ...

  2. 【Linux系统编程】fork()函数详解

    00. 目录 文章目录 00. 目录 01. 进程创建函数 02. 父子进程结构 03. 父子进程地址空间 04. 附录 01. 进程创建函数 #include <sys/types.h> ...

  3. Linux系统编程:fork函数的使用【循环创建N个子线程】

    fork函数介绍 在linux下面进行系统编程,一定要养成一个好习惯,不懂的函数 直接 找男人,用man 指令进行查看,虽然是全英文 但是要强迫自己 学会看英文文档!下面是介绍,我们看重点. FORK ...

  4. 【Linux系统编程】fork() 函数详解

    需要的头文件: #include <sys/types.h> #include <unistd.h> pid_t fork(void); 功能: 用于从一个已存在的进程中创建一 ...

  5. Linux系统编程 46 -lseek函数

    学习笔记 lseek函数 文件偏移 以前有接触到fseek 库函数,lseek和它有点类似. #include <sys/types.h> #include <unistd.h> ...

  6. linux系统编程:暂停函数pause

    pause函数 该函数功能主要是暂停进程,它的返回值总是-1. 使用方式: (1)首先使用signal函数提前注册一个中断函数,该函数用于将函数指针和信号做一个绑定; (2)当程序进行执行pause, ...

  7. Linux系统编程—进程—system函数

    system函数 system()会调用fork()产生子进程,由子进程来调用/bin/sh-c string来执行参数string字符串所代表的命令,此命令执行完后随即返回原调用的进程.在调用sys ...

  8. Linux系统编程—文件—write函数

    一.write函数手册上的定义 write函数需要包含头文件 #include <unistd.h> 函数原型:ssize_t write(int fd, const void *buf, ...

  9. Linux系统编程40:多线程之基于环形队列的生产者与消费者模型

    文章目录 (1)什么是信号量 (2)与信号量相关的操作 (3)基于环形队列的生产者与消费者模型-信号量(单消费者单生产者) (1)什么是信号量 前面的叙述中,我们通过锁保证了每次只有一个线程进入临界区 ...

  10. Linux系统编程:验证kernel内核缓存区大小->4096字节

    Linux系统编程:验证kernel内核缓存区大小->4096字节 李四老师 于 2018-04-04 00:40:04 发布 2778 收藏 2 分类专栏: [Linux编程] [C/C++编 ...

最新文章

  1. 刚子扯个蛋 说下增、删、改、查
  2. 女人赢了 未来500万年男性将灭绝
  3. 拥抱开源!除了微软红帽,这些国际大厂你认识几个?
  4. Alibaba Druid 源码阅读(一) 数据库连接池初步
  5. 工作流之流程定义存储表
  6. 我的dota之路(下)
  7. VMProtect修复导入表的插件
  8. ubuntu完全卸载mysql
  9. SVN汉化包安装后,没有出现对应的语言选项问题解决(附SVN1.12.1汉化包下载地址)
  10. 时间序列学习(1):平稳性、自相关性
  11. 计算机网络中sep是什么意思,SEP系统介绍及实施方案介绍.ppt
  12. word无法创建工作文件请检查临时环境变量
  13. 进程、lwp(轻量级进程)和Java线程的理解
  14. Oracle的OFA架构
  15. python的皮卡丘如何写代码,用python画皮卡丘的代码
  16. Domain generalization 简介
  17. 实时计算业务介绍实时日志分析
  18. 腾讯手机QQ团队无障碍化探索的曲折与收获
  19. LeetCode - 70. 爬楼梯(人肉递归、动态规划)2
  20. c语言输出不足10补0,c++ cout输出不足位补0 setw、setfill

热门文章

  1. 惠普m227fdw引擎通信错误_惠普m227fdw/m132nw提示耗材余量错误解决方案
  2. Linux系统内存管理实验
  3. 简单人物画像_简易人物画像图
  4. B站“崩溃”始末 2021.07.13 我们是这样崩的
  5. HTML 页面 meta 的作用
  6. 云计算发展趋势(二)实现云计算的技术以及其他新兴技术介绍
  7. go编译文件带上图标
  8. java flex 聊天_【转帖】实现了视频私聊功能
  9. outlook配置126邮箱
  10. 记录解决流氓软件无法删除-被资源管理器打开以及被xx程序打开导致无法删除