readv函数和writev函数

在介绍readv函数和writev函数之前,首先需要介绍一个结构体:iovec结构体。

它是C语言中的一个结构体,用于描述一个缓冲区的地址和长度。man文档中对这个结构体的介绍如下:

The pointer iov points to an array of iovec structures, defined in <sys/uio.h> as:

           struct iovec {
               void  *iov_base;    /* Starting address */
               size_t iov_len;     /* Number of bytes to transfer */
           };

iovec结构有两个成员,分别是iov_baseiov_len

        iov_base是一个指针,指向缓冲区的起始地址。

         iov_len是一个size_t类型的整数,表示缓冲区的长度。

通常使用iovec结构数组来操作一组缓冲区,每个iovec结构表示一个缓冲区。

readv函数

readv函数是C语言中的一个系统调用函数,用于从文件描述符中读取数据到多个缓冲区中。

函数原型:

#include <sys/uio.h>

ssize_t readv(int fd, const struct iovec *iov, int iovcnt);

参数说明:

  • fd:文件描述符,表示要读取数据的文件或套接字。
  • iov:一个iovec结构数组,每个结构包含了一个缓冲区的起始地址和长度。
  • iovcnt:缓冲区的数量。

函数的作用是从文件描述符中读取数据,并将数据分散到多个缓冲区中,每个缓冲区的大小由iovec结构的长度字段决定。函数返回实际读取的字节数。

举个栗子:

#include <sys/uio.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>

int main() {
    int fd;
    ssize_t numRead;
    struct iovec iov[3];
    char buf1[10];
    char buf2[20];
    char buf3[30];

    fd = open("test.txt", O_RDONLY);
    if (fd == -1) {
        printf("Failed to open file\n");
        exit(EXIT_FAILURE);
    }

    iov[0].iov_base = buf1;
    iov[0].iov_len = sizeof(buf1);
    iov[1].iov_base = buf2;
    iov[1].iov_len = sizeof(buf2);
    iov[2].iov_base = buf3;
    iov[2].iov_len = sizeof(buf3);

    numRead = readv(fd, iov, 3);

    if (numRead == -1) {
        printf("Failed to read file\n");
        exit(EXIT_FAILURE);
    }

    printf("Read %ld bytes\n", (long) numRead);
    printf("Contents of buf1: %s\n", buf1);
    printf("Contents of buf2: %s\n", buf2);
    printf("Contents of buf3: %s\n", buf3);

    exit(EXIT_SUCCESS);
}

上述代码中,我们首先定义了3个缓冲区buf1、buf2和buf3,然后通过open函数打开了一个文件,并使用readv函数将文件中的内容分散读取到这3个缓冲区中。最后我们打印出每个缓冲区的内容。

需要注意的是,readv函数一般用于读取分散的数据,而writev函数则用于写入集中的数据,其函数原型和用法与readv函数类似。

writev函数

writev函数是C语言中的一个系统调用函数,用于将多个缓冲区的数据写入到文件描述符中。

函数原型:

#include <sys/uio.h>

ssize_t writev(int fd, const struct iovec *iov, int iovcnt);

参数说明:

  • fd:文件描述符,表示要写入数据的文件或套接字。
  • iov:一个iovec结构数组,每个结构包含了一个缓冲区的起始地址和长度。
  • iovcnt:缓冲区的数量。

函数的作用是将多个缓冲区中的数据集中写入到文件描述符中,每个缓冲区的大小由iovec结构的长度字段决定。函数返回实际写入的字节数。

举个栗子:

#include <sys/uio.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>

int main() {
    int fd;
    ssize_t numWritten;
    struct iovec iov[3];
    char buf1[] = "Hello ";
    char buf2[] = "world";
    char buf3[] = "!";

    fd = open("test.txt", O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
    if (fd == -1) {
        printf("Failed to open file\n");
        exit(EXIT_FAILURE);
    }

    iov[0].iov_base = buf1;
    iov[0].iov_len = sizeof(buf1) - 1;  // Exclude null terminator
    iov[1].iov_base = buf2;
    iov[1].iov_len = sizeof(buf2) - 1;
    iov[2].iov_base = buf3;
    iov[2].iov_len = sizeof(buf3) - 1;

    numWritten = writev(fd, iov, 3);

    if (numWritten == -1) {
        printf("Failed to write file\n");
        exit(EXIT_FAILURE);
    }

    printf("Written %ld bytes\n", (long) numWritten);

    exit(EXIT_SUCCESS);
}

上述代码中,我们首先定义了3个缓冲区buf1、buf2和buf3,并给它们赋值。然后通过open函数打开一个文件,使用writev函数将这3个缓冲区的内容集中写入到文件中。最后我们打印出实际写入的字节数。

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值