高级I/O函数readv和writev的用法

本篇文章为大家展示了高级I/O函数readv和writev的用法,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

readv、writev

#include<unistd.h>
#include<sys/uio.h>
#include<fcntl.h>

int main()
{
	int fd = open("aaa",O_RDWR);

	struct iovec buf[2];
	/*
	buf[0].iov_base = malloc(3);
	buf[0].iov_len = 3;
	buf[1].iov_base = malloc(2);
	buf[1].iov_len = 2;
	readv(fd,buf,2);

	printf("%s\n",(char*)buf[0].iov_base);
	printf("%s\n",(char*)buf[1].iov_base);
	*/

	char szbuf[10] = "123";
	buf[0].iov_base = szbuf;
	buf[0].iov_len = strlen(szbuf);

	char szbuf2[10] = "abc";
	buf[1].iov_base = szbuf2;
	buf[1].iov_len = strlen(szbuf2);

	writev(fd,buf,2);

	close(fd);

	return 0;
}

上述内容就是高级I/O函数readv和writev的用法,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注行业资讯频道。