[root@bogon mycode]# cat writev.c
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<sys/uio.h>
int main()
{
char *str1="linux\n";
char *str2="windows\n";
struct iovec iov[2];//创建结构体,该结构体已经在sys/uio.h头文件中定义
iov[0].iov_base=str1;//赋值
iov[0].iov_len=strlen(str1);
iov[1].iov_base=str2;
iov[1].iov_len=strlen(str2);
writev(1,iov,2);//从结构体iov中取两个数据写到(1)输出屏幕中(0代表输入,1代表输出,2代表错误)
return 0;
}
[root@bogon mycode]# gcc writev.c
[root@bogon mycode]# ./a.out
linux
windows
[root@bogon mycode]#
其中iovec结构体如下
struct iovec {
void *iov_base; /* Starting address */
size_t iov_len; /* Number of bytes to transfer */
};
[root@bogon mycode]# gcc readv.c
[root@bogon mycode]# ./a.out
linuxokno
str1 is linuxok
str2 is no [root@bogon mycode]# cat readv.c
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<sys/uio.h>
int main()
{
char buf1[8]={0};
char buf2[8]={0};
struct iovec iov[2];
iov[0].iov_base=buf1;
iov[0].iov_len=sizeof(buf1)-1;//注意是sizeof不是strlen,sizeof的类型是size_t
iov[1].iov_base=buf2;
iov[1].iov_len=sizeof(buf2)-1;
readv(0, iov, 2);//从标准输入读取
printf("str1 is %s\n",buf1);
printf("str2 is %s\n",buf2);
return 0;
}
[root@bogon mycode]#
05-11 18:01