fseek() – C语言库函数
C库函数 int fseek(FILE *stream, long int offset, int whence) 设置流的文件位置给定的偏移量。
声明
以下是 fseek() 函数的声明。
int fseek(FILE *stream,longint offset,int whence)
参数
-
stream -- 这是一个文件对象的标识流的指针。
-
offset -- 这是,以抵消 where 的字节数。
-
whence -- 这是位置偏移量添加。它指定由下列常数之一:
| Constant | 描述 |
|---|---|
| SEEK_SET | Beginning of file |
| SEEK_CUR | Current position of the file yiibaier |
| SEEK_END | End of file |
返回值
如果成功,这个函数返回零,否则返回非零值。
例子
下面的例子演示了如何使用fseek()函数。
#include<stdio.h>int main (){ FILE *fp; fp = fopen("file.txt","w+"); fputs("This is xyhtml5.com", fp); fseek( fp,7, SEEK_SET ); fputs(" C Programming Langauge", fp); fclose(fp);return(0);}
让我们编译和运行上面的程序,这将创建一个包含以下内容的文件file.txt 。最初程序创建文件,并写道: This is xyhtml5.com 但后来我们写指针复位在第七的位置,从一开始使用 puts() 语句写文件包含以下内容:
This is C Programming Langauge