C/C++中的fseek()及其示例

fseek()用于将与给定文件关联的文件指针移动到特定位置。 语法:

null
int fseek(FILE *pointer, long int offset, int position)
pointer: pointer to a FILE object that identifies the stream.
offset: number of bytes to offset from position
position: position from where offset is added.

returns:
zero if successful, or else it returns a non-zero value 

位置定义文件指针需要相对于其移动的点。它有三个价值: SEEK_END:表示文件的结尾。 SEEK_SET:它表示文件的开始。 SEEK_CUR:它表示文件指针的当前位置。

// C Program to demonstrate the use of fseek()
#include <stdio.h>
int main()
{
FILE *fp;
fp = fopen ( "test.txt" , "r" );
// Moving pointer to end
fseek (fp, 0, SEEK_END);
// Printing position of pointer
printf ( "%ld" , ftell (fp));
return 0;
}


输出:

81

解释

文件测试。txt包含以下文本:

"Someone over there is calling you.
we are going for work.
take care of yourself."

当我们实现fseek()时,我们将指针相对于文件结尾移动0距离,即指针现在指向文件结尾。因此,输出为81。

相关文章: C语言中的fseek vs revend

本文由 哈迪克·高尔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞9 分享