1.open 系统调用

说明:
调用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为文件的绝对路径或相对路径。

flag用于指定文件的打开/创建模式,这3个常量定义在fcntl.h中,这3个参数是必选的,而且只能选择一个:
O_RDONLY       只读模式
O_WRONLY      只写模式
O_RDWR         读写模式

下面的常量是可选的:

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 多次,则第一次创建文件,接下来直接打开文件。

运行结果:

  1. yan@yan-vm:~/ctest$ ./a.out
  2. yan@yan-vm:~/ctest$ ll a.txt
  3. -r-------- 1 yan yan 0 Jun  5 07:48 a.txt
  4. yan@yan-vm:~/ctest$ ./a.out
  5. 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 ;
}

运行结果:

  1. yan@yan-vm:~/ctest$ ll a.txt
  2. -rw-rw-r-- 1 yan yan 0 Jun  5 07:55 a.txt
  3. yan@yan-vm:~/ctest$ ./a.out
  4. 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 ;
}

运行结果:

  1. root@yan-virtual-machine:~# ll a.txt
  2. ls: 无法访问a.txt: 没有那个文件或目录
  3. root@yan-virtual-machine:~# ./open
  4. -bash: ./open: 没有那个文件或目录
  5. root@yan-virtual-machine:~# touch a.txt
  6. root@yan-virtual-machine:~# ./a.out
  7. root@yan-virtual-machine:~#

2.creat函数

使用creat函数创建一个新文件,如果原来该文件存在,会将这个文件的长度截短为0。函数定义如下:

#include <fcntl.h>
int creat(const char *pathname, mode_t mode);

如果成功则返回为只写打开的文件描述符,出错则返回-1。

此函数等效于:

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

执行结果为:

  1. yan@yan-vm:~/ctest$ ./a.out
  2. 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函数关闭一个打开的文件。函数定义如下:

  1. #include <unistd.h>
  1. int close(int filedes);  

如果成功返回0,出错返回-1.关闭一个文件时还会释放该进程加在文件上的所有记录锁。

open, create, close的更多相关文章

  1. 记一次tomcat线程创建异常调优:unable to create new native thread

    测试在进行一次性能测试的时候发现并发300个请求时出现了下面的异常: HTTP Status 500 - Handler processing failed; nested exception is ...

  2. 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 ...

  3. 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 错误. 总结一下发生错误的原因,一般的 ...

  4. Create a Team in RHEL7

    SOLUTION VERIFIED September 13 2016 KB2620131 Environment Red Hat Enterprise Linux 7 NetworkManager ...

  5. 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 ...

  6. [转]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 ...

  7. Git异常:fatal: could not create work tree dir 'XXX': No such file or directory

    GitHub实战系列汇总:http://www.cnblogs.com/dunitian/p/5038719.html ———————————————————————————————————————— ...

  8. SQL Server 在多个数据库中创建同一个存储过程(Create Same Stored Procedure in All Databases)

    一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 遇到的问题(Problems) 实现代码(SQL Codes) 方法一:拼接SQL: 方法二: ...

  9. SharePoint 2013 create workflow by SharePoint Designer 2013

    这篇文章主要基于上一篇http://www.cnblogs.com/qindy/p/6242714.html的基础上,create a sample workflow by SharePoint De ...

  10. Create an offline installation of Visual Studio 2017 RC

    Create an offline installation of Visual Studio 2017 RC ‎2016‎年‎12‎月‎7‎日                             ...

随机推荐

  1. JS 08表单操作_表单域

    一.表单的获取方式 document.getElementById() document.forms[index]; document.forms[form_name] document.form_n ...

  2. (一)第一个python语句、乘除法、获取用户输入、函数

    一.print语句 >>> print "hello World!!" python2 和python3 的print是不一样的,python3的print(“h ...

  3. python运行报错:cannot import name 'InteractiveConsole'

    ModuleNotFoundError: No module named '_pydevd_bundle.pydevd_cython' ImportError: cannot import name ...

  4. 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 ...

  5. main函数前后

    void f1(void)__attribute__((constructor)); void f2(void)__attribute__((destructor)); void f1(void) { ...

  6. Oracle学习笔记——Linux下开启Oracle

    1.开启数据库 sqlplus  /  as sysdba startup 2.启动监听:lsnrctl  start; 查看监听状态:lsnrctl status; 3.登入数据库 Linux 设置 ...

  7. JS (二)

    ]1 函数 1 函数就是一段待执行的代码段 2 函数可以实现功能的封装,可以实现代码的复用 3 函数使用: 1 函数声明 2 函数调用 4 语法: 1 函数声明 1 使用function关键字进行函数 ...

  8. 解决在Linux操作系统下无法连接MySQL服务端的问题

    遇到这种问题的时候我们需要考虑的是防火墙规则,因为防火墙默认是禁止所有端口访问的,所以我们需要添加一个访问端口来连接MySQL. 命令如下: 允许某端口   firewall-cmd  --zone= ...

  9. 【Intel 汇编】ELF文件

    ELF文件格式是一个开放标准,各种UNIX系统的可执行文件都采用ELF格式,它有三种不同的类型: 可重定位的目标文件(Relocatable,或者Object File) 可执行文件(Executab ...

  10. Linux教程 Yum命令的使用

    在这篇文章中,我们将学习如何安装,更新,删除,查找安装包,管理安装包以及安装包的仓库在Linux系统使用RedHat开发的YUM(Yellowdog Updater Modified)工具.以下这些命 ...