目录

一.open函数简介:

1.包含的头文件

2.open函数原型

3.函数参数说明:

4.open函数描述

5.打开一个文件(文件存在时),获取文件描述符:

6.打开一个文件,如果需要打开的文件不存在,那么创建该文件:

5. 参数mode为0600时,是什么意思?

二:文件打开创建的补充:

2.1:O_EXCL  的使用

2.2:O_APPEND 的使用

2.3:O_TRUNC  的使用

三:creat函数

3.1函数简介

3.2:运行代码demo,参数mode选择的创建模式是 S_IRWXU。可读、写、执行

3.3:运行结果


一.open函数简介:

1.包含的头文件

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

2.open函数原型

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);

3.函数参数说明:

pathname:要打开的文件名(含路径)

flags : 

 O_RDONLY 只读打开,O_WRONLY 只写打开,O_RDWR 可读可写打开

当我们附带了权限后,打开的文件就只能按照这种权限来操作。

以上这三个参数中应当只指定一个。下列参数是可选择的:

  1. O-CREAT    :若文件不存在则创建它。使用此选项时,需要同时说明第三个参数mode.用其说明该新文件的存取许可权限。
  2. O_EXCL      :以这种属性去打开文件时,如果同时指定了O_CREAT,而文件已经存在,则打开文件失败(也可以说返回值是-1)。
  3. O_APPEND :以这种属性去打开文件时,每次写时都加到文件的尾端。(不想写入时文件被覆盖,用这个flag,正常写入没有加其他条件的话,原来文件中的数据内容会被覆盖,注意是覆盖,覆盖本身的字节长度,没有覆盖的会保留,不是删除)
  4. O_TRUNC   :以这种属性去打开文件时,如果这个文件中本来是有内容的,而且为只读或只写成功打开,则将其长度截短为0,通俗点的意思就是把原来的文件中的内容干掉,写入你自己要的数据内容

Mode:一定是在flags中使用了 O-CREAT 标志,mode 记录待创建的文件的访问权限,(比如:给的是0600时,则文件所有者给的权限是 可读可写)

4.open函数描述

DESCRIPTION
       Given  a  pathname  for  a  file,  open() returns a file descriptor, a small, nonnegative integer for use in subsequent system calls(read(2), write(2), lseek(2), fcntl(2), etc.).  The file descriptor returned by a successful call will be the  lowest-numbered  file descriptor not currently open for the process.

给定一个文件的路径名,open()返回一个文件描述符,一个小的非负整数,用于后续的系统调用(read(2), write(2), Iseek(2), fcntl(2),等等)。成功调用返回的文件描述符将是进程当前未打开的编号最低的文件描述符。失败:返回 -1

5.打开一个文件(文件存在时),获取文件描述符:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>int main()
{int fd;fd = open("./file1",O_RDWR);printf("fd = %d \n",fd);return 0;
}

6.打开一个文件,如果需要打开的文件不存在,那么创建该文件:

【 在参数flags 中使用了 O-CREAT 标志,参数mode 中就要给出文件的访问权限 0600 (或者其他),这两个是配着使用的】

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>int main()
{int fd;fd = open("./file1",O_RDWR);if(fd == -1){printf("open file1 fail \n");fd = open("./file1",O_RDWR|O_CREAT,0600);if(fd > 0){printf("creat file1 success \n");}}printf("open file1 success:fd = %d \n",fd);return 0;
}  

5. 参数mode为0600时,是什么意思?

 对应两个图看:即可知道0600是让我们以文件所有者的身份给了file1文件可读可写的权限。

开头的第一个 - 是普通文件的意思。

二:文件打开创建的补充:

2.1:O_EXCL  的使用

以这种属性去打开文件时,如果同时指定了O_CREAT,而文件已经存在,则打开文件失败(也可以说返回值是-1)。

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>int main()
{int fd;fd = open("./file",O_RDWR|O_CREAT|O_EXCL,0600);if(fd == -1){printf("file1 exist \n");}return 0;
}

2.2:O_APPEND 的使用

以这种属性去打开文件时,每次写时都加到文件的尾端。(不想写入时文件被覆盖,用这个flag,正常写入没有加其他条件的话,原来文件中的数据内容会被覆盖,注意是覆盖,覆盖本身的字节长度,没有覆盖的会保留,不是删除)

首先:没有添加O_APPEND之前:我们手动touch新建一个文件file1,并设置文件file1里面有一堆的内容。

然后我们打开文件的时候只是以可读可写O_RDWR打开。并且向文件file1里面写入一串字符串,代码如下:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>int main()
{int fd;char *buf = "wenjian chu ru men !";fd = open("./file1",O_RDWR);//ssize_t write(int fd, const void *buf, size_t count);int n_write =  write(fd,buf,strlen(buf));printf("write %d byte to file1 \n",n_write);close(fd);return 0;
}

运行结果显示,file1里面的内容被覆盖。

当我们添加O_APPEND使用之后,代码如下:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>int main()
{int fd;char *buf = "wenjian chu ru men !";fd = open("./file1",O_RDWR|O_APPEND);   //在open添加//ssize_t write(int fd, const void *buf, size_t count);int n_write =  write(fd,buf,strlen(buf));printf("write %d byte to file1 \n",n_write);close(fd);return 0;
}

运行结果如下:在文件内容的尾巴另起一行写入字符串。

2.3:O_TRUNC  的使用

以这种属性去打开文件时,如果这个文件中本来是有内容的,而且为只读或只写成功打开,则将其长度截短为0,通俗点的意思就是把原来的文件中的内容干掉,写入你自己要的数据内容

首先,touch新建一个文件file1里面有很多的内容。

然后添加 O_TRUNC使用之后,代码如下:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>int main()
{int fd;char *buf = "test!";fd = open("./file1",O_RDWR|O_TRUNC);   //在open添加//ssize_t write(int fd, const void *buf, size_t count);int n_write =  write(fd,buf,strlen(buf));printf("write %d byte to file1 \n",n_write);close(fd);return 0;
}

运行结果显示,新建的文件file1中的内容被全部删除,并且被写入的字符串取代:

三:creat函数

3.1函数简介

3.2:运行代码demo,参数mode选择的创建模式是 S_IRWXU。可读、写、执行

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>int main()
{int fd;fd = creat("./wenjian",S_IRWXU);return 0;
}

3.3:运行结果

r : 意思是可读

w:意思是可写

x:意思是可执行

对应了代码中,参数mode选择的创建模式是 S_IRWXU。可读、写、执行。

2.文件的打开及创建_open函数_creat函数相关推荐

  1. Linux学习之路4——文件IO打开、创建、读写操作

    1.使用man 2 open.man 2 creat.man 2 write.man 2 read命令获取头文件 语法: int open(const char *pathname, int flag ...

  2. C语言lseek()函数和 fseek()函数 rewind函数

    lseek():移动文件读写位置 头文件: #include <sys/types.h> #include <unistd.h> 定义函数: off_t lseek(int f ...

  3. php+打开文件和其子文件,用 PHP 内置函数 fopen 创建文件和打开文件

    使用 PHP 内置函数 fopen,可以打开一个文件. 打开文件 fopen 最简单语法如下: fopen(filepath,mode) 下面是打开一个文件的 PHP 代码示例: $f = fopen ...

  4. 创建文件 c语言,汇编语言CreateFile函数:创建新文件或者打开已有文件

    函数 CreateFile 可以创建一个新文件或者打开一个已有文件.如果调用成功,函数返回打开文件的句柄:否则,返回特殊常数 INVALID_HANDLE_VALUEO 原型如下: CreateFil ...

  5. createfile调用失败_汇编语言CreateFile函数:创建新文件或者打开已有文件

    函数 CreateFile 可以创建一个新文件或者打开一个已有文件.如果调用成功,函数返回打开文件的句柄:否则,返回特殊常数 INVALID_HANDLE_VALUEO 原型如下: CreateFil ...

  6. c语言open函数打开文件方式,Linux中C语言open函数打开或创建文件详细讲解

    Linux中C语言open函数打开或创建文件详细讲解 Linux中C语言open函数打开或创建文件详细讲解 头文件: #include #include #include 函数原型: int open ...

  7. python创建打开文件-python打开文件方式

    python中的open()函数用于打开一个文件,创建file对象,相关方法才可以调用它进行读写 语法:open(name,[,model[,buffering]]) 模式描述 r 以只读方式打开文件 ...

  8. python创建空文本文件_Python干货:「文件处理整合」 创建、打开、写入和删除...

    什么是Python文件处理? Python文件处理就是如何在python中创建.打开.读取.写入和删除文件的示例.从文件(EXCELSheet.doc文件.文本文件.cv等)或Add中读取数据.是一个 ...

  9. c语言 fgets函数 去除换行符_C语言文件的打开和关闭

    文件代表一系列的字节.C语言提供了标准库函数用于文件的打开和关闭. 1.文件的打开 打开文件的操作通过标准库函数 fopen 完成,该函数定义如下: FILE *fopen( const char * ...

最新文章

  1. c语言怎样定义函数举例,c语言怎样定义函数?
  2. 【hdu 1573 X问题】【 hdu3579 Hello Kiki 】【poj 2891】
  3. 文件特殊权限及facl
  4. 你的女神今日结婚了!!!你失恋了......
  5. 面向切面编程--AOP(二)
  6. 安装SQL2012数据库步骤
  7. C++中的布局new操作符
  8. python简单计算器下载安装到手机_python计算器app下载
  9. 第15届创新英语大赛初赛第二阶段题目
  10. 软件测试(四):软件测试用例设计
  11. docker安装vim命令
  12. 路由器的下一跳计算(网关)
  13. 多少开发人员 饿了么_做个美团(饿了么)网站需要多少钱?
  14. 莫比乌斯反演 平衡规划 双端栈 双端队列 等价类等
  15. EasyExcel注解方式校验数据行
  16. 计算机网络—数据交换方式
  17. 用计算机打字英语单词,计算机基本英语词汇
  18. Java项目:学生综合素质评价系统(java+SSM+thymeleaf+layui+Mysql)
  19. 2017.11.21 软件工程概论第一节课
  20. Chrome实现独立代理

热门文章

  1. 一天一道算法题——799. 香槟塔
  2. 张孝祥java高新技术笔记_张孝祥Java高新技术_课程--------学习笔记第一天
  3. 数字货币即将面世 蹭“数字货币”热度套路频现
  4. acwing-241. 楼兰图腾-树状数组板子题+开脑洞
  5. DragonBall
  6. 常用缓存淘汰算法(LFU、LRU、ARC、FIFO、MRU)
  7. 梦幻春晚服务器找不到,梦幻西游:春节联欢晚会活动抢先看,一幕幕热闹的画面令人回味无穷...
  8. Nodejs进程管理模块forever详解
  9. 每到版本日总是赶通宵,这是个好现象吗?
  10. 差异表达基因提取limma+WGCNA分析全代码