c_cpp writev_readv.c

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp writev_readv.c相关的知识,希望对你有一定的参考价值。

/*================================================
* Author         : Junjie Huang
* Email          : acmhjj@gmail.com
* Last modified  : 2016-02-16 19:14
* Filename       : writev_readv.c
* Description    : readv和write函数让我们在单个函数调用里从多个不连续的缓冲里读入或写出。这些操作被称为分散读(scatter read)和集合写(gather write)
================================================*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/uio.h>
#include <unistd.h>
#include <fcntl.h>
#include <strings.h>

int main (int argc, char const* argv[]){
    if(argc != 2){
        printf("args error!\n");
        return -1;
    }
    int fd = open(argv[1], O_RDWR);
    char buf1[10] = "hello";
    char buf2[10] = "world";
    struct iovec iov[2];
    iov[0].iov_base = buf1;
    iov[0].iov_len = 5;
    iov[1].iov_base = buf2;
    iov[1].iov_len = 5;
    int ret = writev(fd, iov, 2);
    if(-1 == ret){
        perror("writev");
        return -1;
    }
    lseek(fd, 0, SEEK_SET);
    bzero(buf1, sizeof(buf1));
    bzero(buf2, sizeof(buf2));
    ret = readv(fd, iov, 2);
    if(-1 == ret){
        perror("readv");
        return -1;
    }
    printf("buf1 is %s\n", buf1);
    printf("buf2 is %s\n", buf2);
    close(fd);
    return 0;
}

以上是关于c_cpp writev_readv.c的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp 200.岛屿数量

c_cpp 127.单词阶梯

c_cpp MOFSET

c_cpp MOFSET

c_cpp 31.下一个排列

c_cpp string→char *