文章目录
- fseek, _fseeki64
- 作用
- 头文件
- 函数原型
- 参数
- 返回值
- 备注
- 代码示例
fseek, _fseeki64
作用
将文件指针移到指定位置。
头文件
fseek <stdio.h>
_fseeki64 <stdio.h>
函数原型
int fseek(
FILE *stream,
long offset,
int origin
);
int _fseeki64(
FILE *stream,
__int64 offset,
int origin
);
参数
-
stream
指向 FILE 结构的指针。
-
offset
中的字节数 origin 。
-
origin
初始位置。
自变量 origin 必须是下列常量之一,在中定义 stdio.h :
原始值 | 含义 |
SEEK_CUR | 文件指针的当前位置。 |
SEEK_END | 文件结尾。 |
SEEK_SET | 文件开头。 |
返回值
如果成功, fseek 则 _fseeki64 返回0。
否则,返回一个非零值。
在无法查找的设备上,返回值是未定义的。 如果 stream 为 null 指针,或者如果不 origin 是下面所述的允许值之一, fseek 则 _fseeki64 调用无效参数处理程序,如 参数验证中所述。 如果允许执行继续,则这些函数将设置 errno 为 EINVAL 并返回-1。
备注
代码示例

文件中
// crt_fseek.c
// This program opens the file FSEEK.OUT and
// moves the pointer to the file's beginning.
#include <stdio.h>
int main( void )
{
FILE *stream;
char line[81];
int result;
if ( fopen_s( &stream, "fseek.out", "w+" ) != 0 )
{
printf( "The file fseek.out was not opened\n" );
return -1;
}
fprintf( stream, "The fseek begins here: "
"This is the file 'fseek.out'.\n" );
result = fseek( stream, 23L, SEEK_SET);
if( result )
perror( "Fseek failed" );
else
{
printf( "File pointer is set to middle of first line.\n" );
fgets( line, 80, stream );
printf( "%s", line );
}
fclose( stream );
}