zoukankan      html  css  js  c++  java
  • man(2) readv writev

    #include <sys/uio.h>

    ssize_t readv(int fd, const struct iovec *iov, int iovcnt);

    unix高级环境编程中的定义:

    【ssize_t readv(int filedes,const struct iovec iov[ ],int iovcnt) ;
    ssize_t writev(int filedes,const struct iovec iov[ ],int iovcnt) ; 】

    ----------------------------------------------------------------------------

    1 #include <stdio.h>
    2 #include <sys/types.h>
    3 #include <sys/stat.h>
    4 #include <fcntl.h>
    5 #include <sys/uio.h>
    6 #include <unistd.h>
    7
    8 int main()
    9 {
    10 struct iovec iov[2];
    11 char buf[12],str[12];
    12 int fd = open("1.txt",O_RDONLY);
    13 if(fd == -1)
    14 return -1;
    15 iov[0].iov_base = buf;
    16 iov[0].iov_len = 12;
    17 iov[1].iov_base = str;
    18 iov[1].iov_len = 12;
    19 int n = readv(fd, iov, 2);

    buf[11] = 0;//添加这个

    20 printf("n:%d ", n);
    21 //printf("buf:%s ",buf);
    22 //printf("str:%s ",str);
    23 puts(buf);
    24 puts(str);
    25 close(fd);
    26 return 0;
    27 }

    ----------------------------------------------------------------------------

    运行结果很奇怪:第一个缓冲区溢出了

    tarena@ubuntu:~/mon$ ./a.out
    n:23
    buf:aaaaaaaaaaaabbbbbbbbbb

    str:bbbbbbbbbb

    解决办法:

    int main()
    9 {
    10 struct iovec iov[3];
    11 char buf[13],str[13],str2[13];
    12 int fd = open("1.txt",O_RDONLY);
    13 if(fd == -1)
    14 return -1;
    15 iov[0].iov_base = buf;
    16 iov[0].iov_len = 12;
    17 iov[1].iov_base = str;
    18 iov[1].iov_len = 12;
    19 iov[2].iov_base = str2;
    20 iov[2].iov_len = 12;
    21 int n = readv(fd, iov, 3);
    22 buf[12] = 0;
    23 str[12] = 0;
    24 printf("n:%d ", n);
    25 printf("buf:%s ",buf);
    26 printf("str:%s ",str);
    27 printf("str2:%s ",str2);
    28 close(fd);
    29 return 0;
    30 }

    当你要读入12个的时候申请13个存储空间读入的长度iov_len = 12,并且手动在末尾添加字符串结束标志。

    ----------------------------------------------------------------------------

    ssize_t writev(int fd, const struct iovec *iov, int iovcnt);

    ----------------------------------------------------------------------------

    1 #include <stdio.h>
    2 #include <sys/types.h>
    3 #include <sys/stat.h>
    4 #include <fcntl.h>
    5 #include <sys/uio.h>
    6 #include <unistd.h>
    7 #include <string.h>
    8 int main()
    9 {
    10 struct iovec iov[2];
    11 char buf[12] = "hello,lyy",str[12] = "nihao,lyy";
    12 int fd = open("2.txt",O_WRONLY | O_CREAT);
    13 if(fd == -1)
    14 return -1;
    15 iov[0].iov_base = buf;
    16 iov[0].iov_len = strlen(buf);
    17 iov[1].iov_base = str;
    18 iov[1].iov_len = strlen(str);
    19 int n = writev(fd, iov, 2);
    20 printf("n:%d ", n);
    21 close(fd);
    22 return 0;
    23 }

    ----------------------------------------------------------------------------

    运行结果正确,但是为什么需要超级用户才能查看呢?

    tarena@ubuntu:~/mon$ sudo cat 2.txt
    hello,lyynihao,lyytarena@ubuntu:~/mon$

    ---------------------------------------------------------------------------- 

    ssize_t preadv(int fd, const struct iovec *iov, int iovcnt, off_t offset);

    1 #include <stdio.h>
    2 #include <sys/types.h>
    3 #include <sys/stat.h>
    4 #include <fcntl.h>
    5 #include <sys/uio.h>
    6 #include <unistd.h>
    7 int main()
    8 {
    9 int fd = open("4.txt",O_RDWR|O_CREAT);
    10 if(fd == -1) return -1;
    11 struct iovec iov[3];
    12 char str1[12],str2[12],str3[12];
    13 iov[0].iov_base = str1;
    14 iov[0].iov_len = 3;
    15 iov[1].iov_base = str2;
    16 iov[1].iov_len = 4;
    17 iov[2].iov_base = str3;
    18 iov[2].iov_len = 5;
    19 preadv(fd, iov, 3, 4);
    20 str1[iov[0].iov_len] = 0;
    21 str2[iov[1].iov_len] = 0;
    22 str3[iov[2].iov_len] = 0;
    23 printf("str1:%s str2:%s str3:%s ",str1,str2,str3);
    24 close(fd);
    25 return 0;
    26 }

    ssize_t pwritev(int fd, const struct iovec *iov, int iovcnt, off_t offset);

    1 #include <stdio.h>
    2 #include <sys/types.h>
    3 #include <sys/stat.h>
    4 #include <fcntl.h>
    5 #include <sys/uio.h>
    6 #include <unistd.h>
    7 #include <string.h>
    8 int main()
    9 {
    10 int fd = open("4.txt",O_RDWR|O_CREAT);
    11 if(fd == -1) return -1;
    12 struct iovec iov[3];
    13 char str1[12] = "hello,lyy",str2[12] = "nihao,lyy",str3[36] = "nice to meet you";
    14 iov[0].iov_base = str1;
    15 iov[0].iov_len = strlen(str1);
    16 iov[1].iov_base = str2;
    17 iov[1].iov_len = strlen(str2);
    18 iov[2].iov_base = str3;
    19 iov[2].iov_len = strlen(str3);
    20 pwritev(fd, iov, 3, 3);
    21 close(fd);
    22 return 0;
    23 }

    --------------------------------------------------------------------------------------------------------

    这两个举例不太好举

    ssize_t preadv2(int fd, const struct iovec *iov, int iovcnt, off_t offset, int flags);

    flags:RWF_SYNC  RWF_DSYNC

    ssize_t pwritev2(int fd, const struct iovec *iov, int iovcnt, off_t offset, int flags);

    flags:RWF_NOWAI

    ---------------------------------------------

    RWF_HIPRI只用于下列条件

    其中this feature is usable only on a file descriptor opened using the O_DIRECT flag.

    ---------------------------------------------------------------------------------------------------------

    #include <unistd.h>

    ssize_t pread(int fd, void *buf, size_t count, off_t offset);

    从第offset个开始读

    1 #include <stdio.h>
    2 #include <string.h>
    3 #include <unistd.h>
    4 #include <sys/types.h>
    5 #include <sys/stat.h>
    6 #include <fcntl.h>
    7
    8 int main()
    9 {
    10 char buf[12];
    11 int fd = open("3.txt",O_RDONLY);
    12 int n = pread(fd, buf, 12,3);
    13 buf[n] = 0;
    14 printf("n = %d buf:%s ",n,buf);
    15 close(fd);
    16 return 0;
    17 }

    返回值比实际读到的多一个,手动buf[n]置为0

    -------------------------------------------------------------------------------------

    ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset);

    从第offset个开始写,从0开始;(略)

  • 相关阅读:
    JavaScript-Runoob:JavaScript 对象
    JavaScript-Runoob:JavaScript 数据类型
    JavaScript-Runoob:JavaScript 变量
    JavaScript-Runoob:JavaScript 注释
    JavaScript-Runoob:JavaScript 语句
    从word中复制内容包含图片到tinymce编辑器中
    从word中复制内容包含图片到xheditor编辑器中
    从word中复制内容包含图片到kindeditor编辑器中
    从word中复制内容包含图片到ckeditor编辑器中
    从word中复制内容包含图片到fckeditor编辑器中
  • 原文地址:https://www.cnblogs.com/xpylovely/p/11008693.html
Copyright © 2011-2022 走看看