生活随笔
收集整理的這篇文章主要介紹了
lseek函数实现对打开文件的定位
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
LSEEK
文章目錄
lseek函數(shù)
- lseek函數(shù),定位到打開文件的指定位置處
#include <sys/types.h>#include <unistd.h>off_t
lseek(int fd
, off_t offset
, int whence
);
- fd文件描述符
- offset偏移量
- whence在何處偏移
#include <sys/stat.h>
#include <fcntl.h>
#include <ctype.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <errno.h>int main(int argc
, char *argv
[])
{off_t offset
;int fd
;ssize_t numWritten
;fd
= open(argv
[1], O_RDWR
| O_CREAT
,S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
|S_IROTH
| S_IWOTH
); if (fd
== -1)perror("open");offset
= 10000;if (lseek(fd
, offset
, SEEK_SET) == -1)perror("lseek");printf("%s: seek succeeded\n", "10000");numWritten
= write(fd
, "abc", strlen("abc"));printf("write = %ld\n", (long int)numWritten
);if (close(fd
) == -1)perror("close");exit(0);
}
$
touch tfile
$ ./seek_io tfile
10000: seek succeeded
write = 3
$
ls -l tfile
-rw-rw-r-- 1 andrew andrew 10003 5月 25 23:27 tfile
- 說明
- 打開文件之后,從文件開始處偏移10000之后有寫入3個字符,所以為10003
總結(jié)
以上是生活随笔為你收集整理的lseek函数实现对打开文件的定位的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。