readv && writev --- how to use

#include <sys/uio.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>ui

/**open file with checking*/
static inline int open_c(const char *filepath, int mode)
{
        int ret = open(filepath, mode);
        if (ret < 0)
        {
                perror("open file failed");
                exit(-1);
        }
        return ret;
}input

int main(int argc, char *argv[])
{
        if (argc != 3)
        {
                printf("usage: demo [input-file] [output file] \n");
                exit(-1);
        }
        const char *inputfile = argv[1];
        const char *outputfile = argv[2];
        int fd_in = open_c(inputfile, O_RDONLY);
        int fd_out = open_c(outputfile, O_WRONLY);it

        char buf1[256];
        char buf2[512];
        char buf3[1024];io

        struct iovec iov[3] = {
                {buf1, sizeof(buf1)},
                {buf2, sizeof(buf2)},
                {buf3, sizeof(buf3)},
        };
        int ret = readv(fd_in, iov, 3);
        if (ret < 0)
        {
                perror("readv failed");
                exit(-1);
        }
        printf("read %d bytes from file '%s' \n", ret, inputfile);file

        ret = writev(fd_out, iov, 3);
        if (ret < 0)
        {
                perror("writev failed");
                exit(-1);
        }
        printf("write %d bytes to file '%s' \n", ret, outputfile);gc

        return 0;error

}demo

相關文章
相關標籤/搜索