open, create, close
1.open 系统调用
#include <fcntl.h>
int open(const char *pathname, int flag);
int open(const char *pathname, int flag, mode_t mode);//只有新创建文件时才会使用该函数
//返回值,如果成功返回文件描述符,如果出错返回-1
使用open返回的文件描述符作为参数传递给write或read,按照惯例,UNIX中文件描述符0与标准输入相关联,文件描述符1与标准输出相关联,文件描述符2与标准出错输出相关联。依照POSIX标准,0、1、2通常被替换成符号常量STDIN_FILENO、STDOUT_FILENO、STDERR_FILENO(定义在头文件unistd.h中)。文件描述符的范围为0~OPEN_MAX。pathname为文件的绝对路径或相对路径。
下面的常量是可选的:
O_APPEND 每次写操作都写入文件的末尾。
O_CREAT 如果指定文件不存在,则创建这个文件。如果存在则直接打开文件。如果创建新文件,而mode参数没有指定,则创建的文件权限不定。
O_EXCL 如果文件不存在,则返回错误。如果同时指定了O_CREAT,而文件已经存在,则会出错。 用此测试一个文件是否存在,如果不存在,则创建此文件。
O_TRUNC 如果文件存在,并且以只写/读写方式打开,则清空文件全部内容。
O_NOCTTY 如果路径名指向终端设备,不要把这个设备用作控制终端。
O_NONBLOCK 如果路径名指向 FIFO/块文件/字符文件,则把文件的打开和后继 I/O设置为非阻塞模式(nonblocking mode)。
下面三个标志也是可选的,他们是Single UNIX Specification中同步输入和输出选项的一部分:
O_DSYNC 等待物理 I/O 结束后再 write。在不影响读取新写入的数据的前提下,不等待文件属性更新。 //??
O_RSYNC read 等待所有写入同一区域的写操作完成后再进行。
O_SYNC 等待物理 I/O 结束后再 write,包括更新文件属性的 I/O。
可选参数可以和必选参数一起使用,并且可以使用多个,如果要以读写方式打开一个文件,如果该文件已经存在,则
将文件清空,如果没有存在,则新创建文件,flag应该为:O_WRONLY | O_CREAT | O_TRUNC
mode用于在创建新文件时指定文件的权限,参数的:
实践:
如果使用O_CREAT 多次,则第一次创建文件,接下来直接打开文件。
运行结果:
- yan@yan-vm:~/ctest$ ./a.out
- yan@yan-vm:~/ctest$ ll a.txt
- -r-------- 1 yan yan 0 Jun 5 07:48 a.txt
- yan@yan-vm:~/ctest$ ./a.out
- yan@yan-vm:~/ctest$ ./a.out
如果同时使用O_CREAT和O_EXCL,并且文件已经存在,则会出错;如果文件不存在,则创建文件,并且这个操作是原子操作。
#include <stdio.h>
#include <fcntl.h>
int main(void){
int fd;
if((fd = open("./a.txt",O_RDONLY|O_CREAT|O_EXCL))<){
perror("open");
}
close(fd);
return ;
}
运行结果:
- yan@yan-vm:~/ctest$ ll a.txt
- -rw-rw-r-- 1 yan yan 0 Jun 5 07:55 a.txt
- yan@yan-vm:~/ctest$ ./a.out
- open: File exists
如果单独使用O_EXCL,并且文件不存在,则会出错;如果文件已经存在,不会报错,也不会创建文件。
#include <stdio.h>
#include <fcntl.h>
int main(void){
int fd;
if((fd = open("./a.txt",O_RDONLY|O_EXCL))<){
perror("open");
}
close(fd);
return ;
}
运行结果:
- root@yan-virtual-machine:~# ll a.txt
- ls: 无法访问a.txt: 没有那个文件或目录
- root@yan-virtual-machine:~# ./open
- -bash: ./open: 没有那个文件或目录
- root@yan-virtual-machine:~# touch a.txt
- root@yan-virtual-machine:~# ./a.out
- root@yan-virtual-machine:~#
2.creat函数
使用creat函数创建一个新文件,如果原来该文件存在,会将这个文件的长度截短为0。函数定义如下:
#include <fcntl.h>
int creat(const char *pathname, mode_t mode);
如果成功则返回为只写打开的文件描述符,出错则返回-1。
此函数等效于:
- open(pathname, O_WRONLY | O_CREAT | O_TRUNC, mode);
后面的mode_t就是ugo的权限(同open函数),注意,如果creat函数创建了一个可读写的文件,但是因为creat只返回可写的文件描述符,所以使用返回的文件描述符读的时候会出错。例子如下:
#include <stdio.h>
#include <fcntl.h>
int main(void){
int fd;
if((fd = creat("./a.txt", S_IRUSR|S_IWUSR|S_IXUSR))<){
perror("creat");
} char buf[];
if(read(fd,buf,) < ){ //返回的文件描述符不允许读,但是文件的权限还是rwx的
perror("read");
}
close(fd);
return ;
}
执行结果为:
- yan@yan-vm:~/ctest$ ./a.out
- read: Bad file descriptor
如果文件已经存在,再次creat该文件,原来的文件将被删除,重新生成一个空文件。
#include <stdio.h>
#include <fcntl.h>
int main(void){
int fd;
if((fd = creat("./a.txt", S_IRUSR|S_IWUSR|S_IXUSR))<){
perror("creat");
}
close(fd);
return ;
}
运行结果:
root@virtual-machine:~# cat a.txt
123
root@virtual-machine:~# ./a.out
root@virtual-machine:~# cat a.txt
root@virtual-machine:~#
3.close函数
close函数关闭一个打开的文件。函数定义如下:
- #include <unistd.h>
- int close(int filedes);
如果成功返回0,出错返回-1.关闭一个文件时还会释放该进程加在文件上的所有记录锁。
open, create, close的更多相关文章
- 记一次tomcat线程创建异常调优:unable to create new native thread
测试在进行一次性能测试的时候发现并发300个请求时出现了下面的异常: HTTP Status 500 - Handler processing failed; nested exception is ...
- Could not create SSL connection through proxy serve-svn
RA layer request failedsvn: Unable to connect to a repository at URL xxxxxx 最后:Could not create SSL ...
- android 使用Tabhost 发生could not create tab content because could not find view with id 错误
使用Tabhost的时候经常报:could not create tab content because could not find view with id 错误. 总结一下发生错误的原因,一般的 ...
- Create a Team in RHEL7
SOLUTION VERIFIED September 13 2016 KB2620131 Environment Red Hat Enterprise Linux 7 NetworkManager ...
- Create a bridge using a tagged vlan (8021.q) interface
SOLUTION VERIFIED April 27 2013 KB26727 Environment Red Hat Enterprise Linux 5 Red Hat Enterprise Li ...
- [转]nopCommerce Widgets and How to Create One
本文转自:https://dzone.com/articles/what-are-nopcommerce-widgets-and-how-to-create-one A widget is a sta ...
- Git异常:fatal: could not create work tree dir 'XXX': No such file or directory
GitHub实战系列汇总:http://www.cnblogs.com/dunitian/p/5038719.html ———————————————————————————————————————— ...
- SQL Server 在多个数据库中创建同一个存储过程(Create Same Stored Procedure in All Databases)
一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 遇到的问题(Problems) 实现代码(SQL Codes) 方法一:拼接SQL: 方法二: ...
- SharePoint 2013 create workflow by SharePoint Designer 2013
这篇文章主要基于上一篇http://www.cnblogs.com/qindy/p/6242714.html的基础上,create a sample workflow by SharePoint De ...
- Create an offline installation of Visual Studio 2017 RC
Create an offline installation of Visual Studio 2017 RC 2016年12月7日 ...
随机推荐
- JS 08表单操作_表单域
一.表单的获取方式 document.getElementById() document.forms[index]; document.forms[form_name] document.form_n ...
- (一)第一个python语句、乘除法、获取用户输入、函数
一.print语句 >>> print "hello World!!" python2 和python3 的print是不一样的,python3的print(“h ...
- python运行报错:cannot import name 'InteractiveConsole'
ModuleNotFoundError: No module named '_pydevd_bundle.pydevd_cython' ImportError: cannot import name ...
- spingboot启动报驱动Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of th
原因: springboot应用了最新的驱动com.mysql.cj.jdbc.Driver,这个驱动需要用mysql-connector-java包的6.x版本才可以, 而mysql-connect ...
- main函数前后
void f1(void)__attribute__((constructor)); void f2(void)__attribute__((destructor)); void f1(void) { ...
- Oracle学习笔记——Linux下开启Oracle
1.开启数据库 sqlplus / as sysdba startup 2.启动监听:lsnrctl start; 查看监听状态:lsnrctl status; 3.登入数据库 Linux 设置 ...
- JS (二)
]1 函数 1 函数就是一段待执行的代码段 2 函数可以实现功能的封装,可以实现代码的复用 3 函数使用: 1 函数声明 2 函数调用 4 语法: 1 函数声明 1 使用function关键字进行函数 ...
- 解决在Linux操作系统下无法连接MySQL服务端的问题
遇到这种问题的时候我们需要考虑的是防火墙规则,因为防火墙默认是禁止所有端口访问的,所以我们需要添加一个访问端口来连接MySQL. 命令如下: 允许某端口 firewall-cmd --zone= ...
- 【Intel 汇编】ELF文件
ELF文件格式是一个开放标准,各种UNIX系统的可执行文件都采用ELF格式,它有三种不同的类型: 可重定位的目标文件(Relocatable,或者Object File) 可执行文件(Executab ...
- Linux教程 Yum命令的使用
在这篇文章中,我们将学习如何安装,更新,删除,查找安装包,管理安装包以及安装包的仓库在Linux系统使用RedHat开发的YUM(Yellowdog Updater Modified)工具.以下这些命 ...