C语言lseek()函数:移动文件的读写位置
头文件:
|
1
|
#include
|
定义函数:
|
1
|
off_t lseek(int fildes, off_t offset, int whence);
|
参数 whence 为下列其中一种:
- SEEK_SET 参数offset 即为新的读写位置.
- SEEK_CUR 以目前的读写位置往后增加offset 个位移量.
- SEEK_END 将读写位置指向文件尾后再增加offset 个位移量. 当whence 值为SEEK_CUR 或
- SEEK_END 时, 参数offet 允许负值的出现.
返回值:当调用成功时则返回目前的读写位置, 也就是距离文件开头多少个字节. 若有错误则返回-1, errno 会存放错误代码.
附加说明:Linux 系统不允许lseek()对tty 装置作用, 此项动作会令lseek()返回ESPIPE.
C语言fseek()函数:移动文件流的读写位置
头文件:
|
1
|
#include
|
定义函数:
|
1
|
int fseek(FILE * stream, long offset, int whence);
|
返回值:当调用成功时则返回0, 若有错误则返回-1, errno 会存放错误代码.
附加说明:fseek()不像lseek()会返回读写位置, 因此必须使用ftell()来取得目前读写的位置.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
#include
main()
{
FILE * stream;
long offset;
fpos_t pos;
stream = fopen("/etc/passwd", "r");
fseek(stream, 5, SEEK_SET);
printf("offset = %d\n", ftell(stream));
rewind(stream);
fgetpos(stream, &pos);
printf("offset = %d\n", pos);
pos = 10;
fsetpos(stream, &pos);
printf("offset = %d\n", ftell(stream));
fclose(stream);
}
|
|
1
2
3
|
offset = 5
offset = 0
offset = 10
|
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/205234.html原文链接:https://javaforall.net
