日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

pread介绍

發布時間:2024/1/23 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 pread介绍 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.先來介紹pread函數

[root@bogon mycode]# cat test.c #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> char buf[20]; void testpread(int fd1) {int i;printf("use pread\n");pread(fd1, buf, 3, 2);//起始位置為2,偏移量為3,總的意思就是從fd1文件描述符中的起始位置//為2到偏移量為3的內容讀取到buf中,注意執行后文件偏移量沒有變動,//所以下面的第一條read語句,起始位置還是開頭那里for (i=0; i<3; i++) printf("%c", buf[i]);read(fd1, buf, 3);for (i=0; i<3; i++)printf("%c", buf[i]);printf("\nuse read\n");read(fd1, buf, 3); //上一次read使得文件偏移量移動了3個位置,所以打印的是456for (i=0; i<3; i++) printf("%c", buf[i]); }int main() {int fd, fd1, i;fd1 = open("linux.txt", O_RDWR);testpread(fd1);close(fd1);return 0; } [root@bogon mycode]# cat linux.txt 123456 [root@bogon mycode]# gcc test.c [root@bogon mycode]# ./a.out use pread 345123 use read 456 [root@bogon mycode]#

2.再介紹pwrite

[root@bogon mycode]# cat linux.txt 123456 [root@bogon mycode]# cat test.c #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <fcntl.h> char buf[20]; char name[] = "linuxfiletest"; void testpwrite(int fd1) {int i;pwrite(fd1, name, 5, 0);//從name中取5個字節從fd1的起始0位置開始寫入read(fd1, buf, 5);//pwrite不會改變文件偏移量,所以這里還是從頭開始打印的for (i=0; i<5; i++) printf("%c", buf[i]);printf("\n"); } int main() {int fd, fd1, i;fd1 = open("linux.txt", O_RDWR);testpwrite(fd1);close(fd1);return 0; } [root@bogon mycode]# gcc test.c [root@bogon mycode]# ./a.out linux [root@bogon mycode]# cat linux.txt linux6//文件內容被修改了 [root@bogon mycode]#

參考鏈接:https://blog.csdn.net/qq_34829953/article/details/72725406

總結

以上是生活随笔為你收集整理的pread介绍的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。