首页 > 编程知识 正文

fseek函数可以实现的操作,fseek函数的功能

时间:2023-05-06 16:47:27 阅读:203813 作者:3320

fseek函数

说明:本文主要是对man 帮助命令的翻译,若有错误,欢迎指正。

在linux学习过程中,如果我们遇到一个陌生函数,都会想赶快查一下,你通常是怎么查询linux系统函数和命令的使用方法呢?google?百度?还是使用man命令?其实对于一个初级程序员,学习使用man命令是很有益的,我们可能看到很多网上的说明都是对man帮助文档的翻译。所以,如果你英文不是很差,就自己来翻译一下你想要查询的信息吧!

今天我们就通过这样的方式学习一下fseek这个函数。

打开linux的控制台,我们输入“man fseek”:会进入帮助文档,里面关于fseek的说明是这样的:


FSEEK(3) Linux Programmer's Manual FSEEK(3)NAME fgetpos, fseek, fsetpos, ftell, rewind - reposition a streamSYNOPSIS #include <stdio.h> int fseek(FILE *stream, long offset, int whence); long ftell(FILE *stream); void rewind(FILE *stream); int fgetpos(FILE *stream, fpos_t *pos); int fsetpos(FILE *stream, fpos_t *pos);DESCRIPTION The fseek() function sets the file position indicator for the stream pointed to by stream. The new position, measured in bytes, is obtained by adding offset bytes to the position specified by whence. If whence is set to SEEK_SET, SEEK_CUR, or SEEK_END, the offset is relative to the start of the file, the current position indicator, or end-of-file, respectively. A successful call to the fseek() function clears the end-of-file indicator for the stream and undoes any effects of the ungetc(3) function on the same stream. The ftell() function obtains the current value of the file position indicator for the stream pointed to by stream. The rewind() function sets the file position indicator for the stream pointed to by stream to the begin‐ xddjz of the file. It is equivalent to: (void) fseek(stream, 0L, SEEK_SET) except that the error indicator for the stream is also cleared (see clearerr(3)). The fgetpos() and fsetpos() functions are alternate interfaces equivalent to ftell() and fseek() (with whence set to SEEK_SET), setting and storing the current value of the file offset into or from the object referenced by pos. On some non-UNIX systems an fpos_t object may be a complex object and these routines may be the only way to portably reposition a text stream.RETURN VALUE The rewind() function returns no value. Upon successful completion, fgetpos(), fseek(), fsetpos() return 0, and ftell() returns the current offset. Otherwise, -1 is returned and errno is set to indi‐ cate the error.ERRORS EBADF The stream specified is not a seekable stream. EINVAL The whence argument to fseek() was not SEEK_SET, SEEK_END, or SEEK_CUR. The functions fgetpos(), fseek(), fsetpos(), and ftell() may also fail and set errno for any of the errors specified for the routines fflush(3), fstat(2), lseek(2), and malloc(3).CONFORMING TO C89, C99.SEE ALSO lseek(2), fseeko(3)COLOPHON This page is part of release 3.35 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://man7.org/linux/man-pages/.GNU 1993-11-29 FSEEK(3)

让我们一起理解和翻译一下吧:

linux程序员使用手册中,把fgetpos,fseek,fsetpos,ftell,rewind几个函数放在一起介绍的。因为他们都和重新定位流有关。我们只详细了解一下fseek。

概要:

fseek函数在<stdio.h>头文件中定义。

原型是:int fseek(FILE *stream, long offset, int whence);

说明:

fseek()函数重新定位了指向文件的指示符的位置。通常情况下,当我们打开一个文件时,指示符定位在文件的第一个字节处。

通过fseek函数重新定位指示符时是以字节为单位的。

stream表示已经打开的某个文件描述符,

offset表示偏移量的大小,也就是字节的个数。

whence表示相对位置,取值可以是SEEK_SET, SEEK_CUR, SEEK_END三种。

​ SEEK_SET(值为0)表示重新定位到文件开始后offset字节位置。

​ SEEK_CUR(值为1)表示重新定位到当前位置后的offset字节位置。

​ SEEK_END(值为2)表示重新定位到文件尾后的offset字节位置,此时允许出现负数,表示倒数第offset个字节处。

一次成功的fseek函数调用,会清除文件尾的指示符,并不会对ungetc函数的调用起到任何影响。

返回值

调用成功返回0,不成功返回-1。不成功时,指示符的位置不变。造成不成功的原因可能是offset已经超出了文件大小。或者指向的流是不可搜索的。

测试代码

#include <stdio.h>#include <stdlib.h>int main(int argc,char *argv[]){ int rtn=-1; char cur=' '; FILE * fp = fopen("test.txt", "r+"); if(fp == NULL) { printf("file unreablen"); return -1; } else { rtn=fseek(fp,3L, SEEK_SET);//重定位到文件开头后3个字节处 printf("nrtn=%dn",rtn); cur=fgetc(fp); //获取当前位置的字符,并将指示符移动一个字节 printf("ncur=%cn",cur); rtn=fseek(fp,3L, SEEK_CUR);//重定位到当前指示符后3个字节处 printf("nrtn=%dn",rtn); cur=fgetc(fp); //获取当前位置的字符,并将指示符移动一个字节 printf("ncur=%cn",cur); rtn=fseek(fp,-3L, SEEK_END);//重定位到倒数第3个字节处 printf("nrtn=%dn",rtn); cur=fgetc(fp); //获取当前位置的字符,并将指示符移动一个字节 printf("ncur=%cn",cur); fclose(fp); return 0; }} test.txt的内容:abcdefghijklmn 运行结果:rtn=0cur=drtn=0cur=hrtn=0cur=m

了解了这些,快自己使用一下fseek吧。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。