Linux系统调用二、open()函数与close()函数介绍

简介: Linux系统调用二、open()函数与close()函数介绍

❀1. open函数

  • 包含头文件
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
  • 函数原型
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
  • 函数功能
    打开一个文件,并返回文件描述符。
  • 函数参数
  • pathname:文件名
  • flags:
  • 必选参数(下面三个必须要有一个)
  • O_RDONLY :只读
  • O_WRONLY :只写
  • O_RDWR :可读可写
  • 可选参数(仅列出常用参数)
  • O_APPEND :追加的方式打开,The file is opened in append mode.
  • O_CREAT :如果文件不存在则创建,If the file does not exist it will be created.
  • O_EXCL :和O_CREAT一块使用,如果文件存在则报错,if this flag is specified in conjunction with O_CREAT, and pathname already exists, then open() will fail.
  • O_NONBLOCK :非阻塞的方式打开文件
  • mode:权限位 (实际的权限是mode & ~umask的结果)
  • 函数返回值
    返回最小的空闲的文件描述符,如果失败则返回-1并设置errno,fopen() and creat() return the new file descriptor, or -1 if an error occurred (in which case, errno is set appropriately).

❀2. close函数

  • 包含头文件
#include <unistd.h>
  • 函数原型
int close(int fd);
  • 函数功能
    close() closes a file descriptor, so that it no longer refers to any file and may be reused.
  • 函数参数
    fd :一个文件的文件描述符
  • 函数返回值
    成功返回0,失败返回-1且设置errno,close() returns zero on success. On error, -1 is returned, and errno is set appropriately.

❀3. 使用open与close实现touch命令

/************************************************************
  >File Name  : mtouch.c
  >Author     : QQ
  >Company    : QQ
  >Create Time: 2022年05月12日 星期四 19时48分14秒
************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, char* argv[])
{
    if(argc < 2)
    {
        printf("not found file name\n");
        return -1;
    }
    int i = 0;
    for(i = 1; i < argc; i++)
    {
        int fd = open(argv[i], O_RDONLY | O_CREAT, 0666);
        close(fd);
    }
    return 0;
}

实际上main函数也是有参数和返回值的,只不过我们在平时的学习中可能很少用到,main的返回值是int类型的,main函数的参数在Linux下编程用的还是比较多的。我们在运行一个可执行文件的时候可以在命令行传入参数给argv[],也就是说argv[]是用来存放我们在命令行传入的参数的,而参数argc用于统计参数的个数。不管我们传不传参数, argv[0]默认就是程序运行的路径名。也就是说argc最小为1(命令行不传参),argv[0]是程序运行路径。



相关文章
|
1天前
|
Linux 编译器 调度
xenomai内核解析--双核系统调用(二)--应用如何区分xenomai/linux系统调用或服务
本文介绍了如何将POSIX应用程序编译为在Xenomai实时内核上运行的程序。
15 1
xenomai内核解析--双核系统调用(二)--应用如何区分xenomai/linux系统调用或服务
|
3天前
|
存储 算法 网络协议
【探索Linux】P.26(网络编程套接字基本概念—— socket编程接口 | socket编程接口相关函数详细介绍 )
【探索Linux】P.26(网络编程套接字基本概念—— socket编程接口 | socket编程接口相关函数详细介绍 )
10 0
|
3天前
|
消息中间件 Unix Linux
【探索Linux】P.14(进程间通信 | 匿名管道 | |进程池 | pipe() 函数 | mkfifo() 函数)
【探索Linux】P.14(进程间通信 | 匿名管道 | |进程池 | pipe() 函数 | mkfifo() 函数)
10 0
|
3天前
|
Linux Shell
Linux中system函数
Linux中system函数
7 0
|
9天前
|
Linux Shell 调度
【Linux系列】fork( )函数原理与应用详解——了解【父子进程及其特性】(代码演示,画图帮助理解,思维导图,精简)(11)
【Linux系列】fork( )函数原理与应用详解——了解【父子进程及其特性】(代码演示,画图帮助理解,思维导图,精简)(11)
|
17天前
|
算法 Linux Shell
【linux进程(二)】如何创建子进程?--fork函数深度剖析
【linux进程(二)】如何创建子进程?--fork函数深度剖析
|
1月前
|
Linux 开发者
Linux文件编程(open read write close函数)
通过这些函数,开发者可以在Linux环境下进行文件的读取、写入和管理。 买CN2云服务器,免备案服务器,高防服务器,就选蓝易云。百度搜索:蓝易云
93 4
|
1天前
|
安全 Linux 测试技术
|
1天前
|
安全 Linux Windows
Linux中Shutdown命令使用介绍
Linux中Shutdown命令使用介绍
|
2天前
|
缓存 关系型数据库 Linux
Linux目录结构:深入理解与命令创建指南
Linux目录结构:深入理解与命令创建指南