Linux系统文件读写的系统调用


一、文件的打开与关闭

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:read-only
    • O_WRONLY:write-only
    • O_RDWR:read/write

    以下摘自手册

    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, respec‐tively.

    • O_CREAT:加上此参数后,如果目标文件不存在则会创建文件。并且增加 mode 给出的权限。

返回值:标识文件的文件描述符,打开失败返回-1

2.close

头文件:

#include <unistd.h>

声明:

int close(int fd);

参数:文件描述符

返回值:关闭成功返回0,否则返回-1

close() returns zero on success. On error, -1 is returned, and errno is set appropriately.

二、文件的读和写

1.read

头文件:

#include <unistd.h>

声明:

ssize_t read(int fd, void *buf, size_t count);

参数:

  • fd:文件描述符
  • buf:存放读取结果的数组
  • count:数组长度。

返回值:

  • 在系统文件中可以发现这两条语句,从中可以看出,ssize_t为long类型。
typedef __ssize_t ssize_t;
typedef long __ssize_t;
  • 返回读取到的字符数量。当返回值为0时表示文件读指针移至末尾。错误返回-1.

2.write

头文件:

#include <unistd.h>

声明:

ssize_t write(int fd, const void *buf, size_t count);

返回值:成功写入则返回写入的字节数,0代表什么都没写入。错误返回-1.

On success, the number of bytes written is returned (zero indicates nothing was written). It is not an error if this number is smaller than the number of bytes requested; this may happen for example because the disk device was filled. See also NOTES.

三、文件指针的移动

lseek

头文件:

#include <sys/types.h>
#include <unistd.h>

声明:

off_t lseek(int fd, off_t offset, int whence);

参数:

lseek() repositions the file offset of the open file description associated with the file descriptor fd to the argument offset according to the directive whence as follows:

  • SEEK_SET
  • SEEK_CUR
  • SEEK_END
  • offset:偏移量
  • whence:从哪里开始计算偏移量
    • SEEK_SET:文件指针被设置为参数offset
    • SEEK_CUR:文件指针被设置为当前位置加参数offset
    • SEEK_END:文件指针被设置为文件末尾加参数offset

返回值:返回当前文件指针的位置,失败则返回-1.

基于上述,有了如下的几种使用方式:

  • lseek(fd, 0, SEEK_SET);	//从头开始读写文件
    lseek(fd, 0, SEEK_SET);	//返回当前文件指针位置
    lseek(fd, 0, SEEK_END); //将指针移至文件末尾,即获取文件长度。
    

5.演示程序

以下代码实现了文件 h e y . t x t hey.txt hey.txt中的内容复制到文件 t a r g e t . t x t target.txt target.txt文件中。

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
int main()
{
    int fdRead = open("hey.txt", O_RDONLY);
    int fdWrite = open("target.txt", O_RDWR | O_CREAT, 0777);
    if(fdRead == -1 || fdWrite == -1){
        perror("open");
    }

    char buffer[1024];

    int len = 1;
    while(len > 0){
        len = read(fdRead, buffer, sizeof(buffer));
        write(fdWrite, buffer, len);
    }

    close(fdRead);
    close(fdWrite);
    return 0;
}

以下代码实现了文件 r e a d y . t x t ready.txt ready.txt的文件长度扩大10倍。

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
int main()
{
    int fd = open("ready.txt", O_RDWR);
    if(fd == -1){
        perror("open");
        return -1;
    }
    int len = lseek(fd, 0, SEEK_END);
    lseek(fd, len * 10, SEEK_END);
    write(fd, " ", 1);					//只是扩大10倍并不能看到文件属性的更改,只有在最后写入一个空格才可以看到。
    close(fd);
    return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值