```cpp
#include <sys/uio.h>
ssize_t readv(int fd, const struct iovec *iov, int iovcnt);
ssize_t writev(int fd, const struct iovec *iov, int iovcnt);
struct iovec {
void *iov_base; /* Starting address */
size_t iov_len; /* Number of bytes to transfer */
};
The readv() system call reads iovcnt buffers from the file associated with the file descriptor fd into the buffers described by iov ("scatter input").
The writev() system call writes iovcnt buffers of data described by iov to the file associated with the file descriptor fd ("gather output").
```
```cpp
#include <unistd.h>
#include <sys/uio.h>
#include <stdio.h>
#include <fcntl.h>
int main(int argc, char *argv[])
{
ssize_t iSize;
char acBuf1[9];
char acBuf2[9];
struct iovec iov[2];
int iFd1 = open(argv[1], O_RDONLY);
int iFd2 = open(argv[2], O_RDONLY);
int iFd3 = open(argv[3], O_WRONLY);
iSize = read(iFd1, acBuf1, sizeof(acBuf1));
iSize = read(iFd2, acBuf2, sizeof(acBuf2));
iov[0].iov_base = acBuf1;
iov[0].iov_len = sizeof(acBuf1);
iov[1].iov_base = acBuf2;
iov[1].iov_len = sizeof(acBuf2);
iSize = writev(iFd3, iov, 2);
close(iFd1);
close(iFd2);
close(iFd3);
return 0;
}
```
建立3个文件(test1, test2, test3)
test1写入12345
test2写入asdfghi
test3为空
运行
./a.out test1 test2 test3
相关文章
- linux高级IO之readv/writev
- 《unix高级环境编程》高级 I/O—— readv 和 writev 函数
- Unix系统编程():分散输入和集中输出(Scatter-Gather IO):readv和writev
- Linux:何时使用分散/收集IO (readv, writev)与使用fread的大型缓冲区
- 《UNIX环境高级编程》笔记--readv和writev函数
- linux网络编程七:分散读readv和集中写writev
- linux网络编程七:分散读readv和集中写writev
- readv/writev系统调用
- winapi有没有类似unix的readv/writev(分散读/会聚写)的功能调用?
- Unix系统编程():分散输入和集中输出(Scatter-Gather IO):readv和writev