fseek、_fseeki64

Moves the file pointer to a specified location.

int fseek( 
   FILE *stream,
   long offset,
   int origin 
);
int _fseeki64( 
   FILE *stream,
   __int64 offset,
   int origin 
);

参数

  • stream
    指向 FILE 结构的指针。

  • offset
    Number of bytes from origin.

  • origin
    Initial position.

返回值

If successful, fseek and _fseeki64 returns 0. 否则,它返回一个非零值。 On devices incapable of seeking, the return value is undefined. If stream is a null pointer, or if origin is not one of allowed values described below, fseek and _fseeki64 invoke the invalid parameter handler, as described in 参数验证. 如果允许执行继续,则这些功能将 EINVAL 设置为 errno 并返回 -1。

备注

The fseek and _fseeki64 functions moves the file pointer (if any) associated with stream to a new location that is offset bytes from origin*.*The next operation on the stream takes place at the new location. On a stream open for update, the next operation can be either a read or a write. The argument origin must be one of the following constants, defined in STDIO.H:

  • SEEK_CUR
    Current position of file pointer.

  • SEEK_END
    文件尾。

  • SEEK_SET
    Beginning of file.

You can use fseek and _fseeki64 to reposition the pointer anywhere in a file. The pointer can also be positioned beyond the end of the file. fseek and _fseeki64clears the end-of-file indicator and negates the effect of any prior ungetc calls against stream.

When a file is opened for appending data, the current file position is determined by the last I/O operation, not by where the next write would occur. If no I/O operation has yet occurred on a file opened for appending, the file position is the start of the file.

For streams opened in text mode, fseek and _fseeki64have limited use, because carriage return–linefeed translations can cause fseek and _fseeki64to produce unexpected results. The only fseek and _fseeki64operations guaranteed to work on streams opened in text mode are:

  • Seeking with an offset of 0 relative to any of the origin values.

  • Seeking from the beginning of the file with an offset value returned from a call to ftell when using fseekor _ftelli64when using_fseeki64.

Also in text mode, CTRL+Z is interpreted as an end-of-file character on input. In files opened for reading/writing, fopen and all related routines check for a CTRL+Z at the end of the file and remove it if possible. 这是因为使用 fseek 和 ftell或_fseeki64 和_ftelli64的结合, 在以 CTRL+Z 结尾的文件中移动时,可能导致 fseek 或 _fseeki64 在文件末尾错误运行。

当打开 CRT 启动与字节顺序标记 (BOM) 的文件时,文件指针指向(BOM)尾 (即在文件的实际内容的开头)。 If you have to fseek to the beginning of the file, use ftell to get the initial position and fseek to it rather than to position 0.

This function locks out other threads during execution and is therefore thread-safe. 有关非固定版本,请参见 _fseek_nolock、_fseeki64_nolock

要求

功能

必需的标头

fseek

<stdio.h>

_fseeki64

<stdio.h>

有关其他兼容性信息,请参见“简介”中的兼容性

示例

// 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 );
}
  

.NET Framework 等效项

请参见

参考

流 I/O

fopen、_wfopen

ftell、_ftelli64

_lseek、_lseeki64

rewind