文件 IO

本文详细介绍了Linux系统中如何进行文件操作,包括打开和创建文件(open)、设置文件权限(mode)、读写文件(read、write)、关闭文件(close)以及重置文件偏移量(lseek)。还提到了目录读取(readlink)和获取文件状态(file status)的操作。这些基本文件操作是系统编程和日常文件管理的基础。
摘要由CSDN通过智能技术生成

open/close

打开文件 open and possibly create a file or device - 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)

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. 打开或者创建一个文件

参数:

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

  • The argument flags must include one of the following access modes: O_RDONLY, O_WRONLY, or O_RDWR. These request opening the file read-only, write-only, or read/write, respectively.

  • In addition, zero or more file creation flags and file status flags can be bitwise-or’d in flags. The file creation flags are O_CLOEXEC, O_CREAT, O_DIRECTORY, O_EXCL, O_NOCTTY, O_NOFOLLOW, O_TRUNC, and O_TTY_INIT.

  • O_RDONLY:只读方式打开文件。

    O_WRONLY:可写方式打开文件。

    O_RDWR:读写方式打开文件。

    以上三个参数互斥

  • O_CREAT: If the file does not exist it will be created. (如果该文件不存在,就创建一个新的文件,并用第三的参数为其设置权限。)

    • mode specifies the permissions to use in case a new file is created. This argument must be supplied when O_CREAT is specified in flags; if O_CREAT is not specified, then mode is ignored. The effective permissions
      are modified by the process’s umask in the usual way: The permissions of the created file are (mode & ~umask). Note that this mode applies only to future accesses of the newly created file; the open() call that creates a read-only file may well return a read/write file descriptor.
    • The following symbolic constants are provided for mode: (man 2 open)
  • O_EXCL: Ensure that this call creates the file: if this flag is specified in conjunction with O_CREAT, and pathname already exists, then open() will fail. (这一参数可测试文件是否存在)

  • O_TRUNC: If the file already exists and is a regular file and the open mode allows writing (i.e., is O_RDWR or O_WRONLY) it will be truncated to length 0.

  • O_APPEND: The file is opened in append mode. Before each write(2), the file offset is positioned at the
    end of the file
    , as if with lseek(2). O_APPEND may lead to corrupted files on NFS filesystems if more than one process appends data to a file at once. This is because NFS does not support appending to a file, so the client kernel has to simulate it, which can’t be done without a race condition.

  • mode:一定是在 flags 中使用了 O_CREAT 标志,mode 记录待创建文件的访问权限。八进制数表示 0666 所创建出的文件权限与 umask(文件权限掩码)(取反之后与)后得到最终的文件权限

    • umask 0000:将 umask 值改为 0000

返回值:open() returns the new file descriptor, or -1 if an error occurred (in which case, errno is set appropriately).

创建文件 creat

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
``int creat(const char *pathname, mode_t mode)`

DESCRIPTION: 创建一个文件,并以只写的方式打开该文件

参数:

  • pathname:创建的文件名(含路径)
  • mode:创建的文件的读写权限

返回值:

  • 成功:文件描述符
  • 失败:-1
...
    
	fd = creat("/home/linux/文档/test2.c", 0621)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值