writev/readv
2016-02-23 09:41
323 查看
```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
来自为知笔记(Wiz)
#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
来自为知笔记(Wiz)
相关文章推荐
- JQuery在iframe中实现 点击后选中当前栏目的样式
- 深入理解C语言
- JAVA_面向对象_练习_封装_02_EncapsulationDaemon02.java
- IOS性能调优系列:Analyze静态分析
- JAVA_面向对象_练习_封装_02_EncapsulationDaemon02.java
- VS2008删除起始页最近打开项目列表
- C++中define与const的区别
- ios获取手机连接的无线网名字
- adb shell模拟点击事件 input
- Struts2动态方法调用
- Swift-数组
- Android屏幕适配全攻略(最权威的官方适配指导)
- Apple Pay 应用内支付流程分析
- Web容器初始化时获取bean的几种方法
- 常用代码备份--字符串处理
- 读取本地数据库文件
- iOS9下有关CoreLocation学习二
- JIRA 6.3.6在Linux下的安装部署
- 共享MFC DLL的规则DLL 、带静态链接MFC的规则DLL和MFC扩展DLL区别
- VS2013试用期结束后如何激活