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日 ...
随机推荐
- Manacher算法+注释
Manacher算法是用来求一个字符串中最长回文串的算法. 考虑暴力求最长回文串的做法: 暴力枚举字符串中的所有字串判断是否回文,然后求最大值. 时间复杂度O(n^3),考虑优化. 我们从枚举所有字串 ...
- JS基础_构造函数修改
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- JS基础_使用工厂方法创建对象(了解下就行了,用的不多)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 货币转换B
描述 人民币和美元是世界上通用的两种货币之一,写一个程序进行货币间币值转换,其中: ...
- 如何结合插件 vue-lazyload 来简单实现图片懒加载?
插件地址:https://www.npmjs.com/package/vue-lazyload: 一.使用场景: 在项目中有很多条数的信息,且图片很多的时候,不需要一次把整个页面的图片都加载完,而是在 ...
- JavaScript引入
三种引入方式 js标签引入的三种方式 1.行间式 写在标签的事件属性中 <div onclick="alert('hello')"></div>(点击出弹窗 ...
- 关于NavigationBar的笔记
1常用几个方法 全局 //设置navigationBar 的类型 ,ps: status bar的状态受navigationbar控制(当用navigationcontroller时,通过设置此属性改 ...
- Python学习记录8-继承2
继承 单继承和多继承 单继承:每个类只能继承一个类 多继承:每个类允许继承多个类 >>> class A(): pass >>> class B(A): pass ...
- mysql的unsigned属性负值报错和为0情况及mysql的严格模式
最近发现在进行线程操作时,发现数据库的unsigned字段减为负数时并未报错而是变为0,因此去寻找解决方案,发现这和我的sql_mode有关. sql_mode MySQL服务器可以以不同的SQL模式 ...
- [Selenium3+python3.6]自动化测试2-入门
参考http://www.cnblogs.com/yoyoketang/p/6123890.html #coding=utf-8 #Import webdriver time module from ...