[英]Read/write from file descriptor at offset
有pread / pwrite函數接受文件偏移量:
ssize_t pread(int fd, void *buf, size_t count, off_t offset);
ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset);
是。 您在同一個庫中使用lseek
函數。
然后,您可以搜索相對於文件開頭或結尾或相對於當前位置的任何偏移量。
不要被那個圖書館頁面所淹沒。 以下是一些簡單的用法示例,可能是大多數人都需要的:
lseek(fd, 0, SEEK_SET); /* seek to start of file */
lseek(fd, 100, SEEK_SET); /* seek to offset 100 from the start */
lseek(fd, 0, SEEK_END); /* seek to end of file (i.e. immediately after the last byte) */
lseek(fd, -1, SEEK_END); /* seek to the last byte of the file */
lseek(fd, -10, SEEK_CUR); /* seek 10 bytes back from your current position in the file */
lseek(fd, 10, SEEK_CUR); /* seek 10 bytes ahead of your current position in the file */
祝好運!
是的,你正在尋找lseek
。
lseek()
,你們將得到。
是的,你可以使用lseek()
:
off_t lseek(int fd, off_t offset, int whence);
所述
lseek()
函數重新定位偏移與文件描述符關聯打開的文件的fd
的說法offse
根據指令噸whence
如下:
SEEK_SET
偏移量設置為偏移字節。
SEEK_CUR
偏移量設置為其當前位置加上偏移字節。
SEEK_END
偏移量設置為文件大小加上偏移字節。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.