簡體   English   中英

在偏移處從文件描述符讀/寫

[英]Read/write from file descriptor at offset

我一直在使用read(2)write(2)函數來讀取和寫入給定文件描述符的文件。

是否有這樣的函數允許您將偏移量放入文件中進行讀/寫?

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

http://linux.die.net/man/2/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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM