fseek、_fseeki64fseek, _fseeki64

将文件指针移到指定位置。Moves the file pointer to a specified location.

语法Syntax

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

参数Parameters

stream
指向文件结构的指针。Pointer to FILE structure.

offsetoffset
origin 中的字节数。Number of bytes from origin.

originorigin
初始位置。Initial position.

返回值Return Value

如果成功, fseek_fseeki64返回 0。If successful, fseek and _fseeki64 returns 0. 否则,返回一个非零值。Otherwise, it returns a nonzero value. 在无法查找的设备上,返回值是未定义的。On devices incapable of seeking, the return value is undefined. 如果是 null 指针,或如果原点不是允许的值,如下所述之一fseek_fseeki64调用无效参数处理程序,如中所述参数验证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 Parameter Validation. 如果允许执行继续,则这些函数将设置errnoEINVAL并返回-1。If execution is allowed to continue, these functions set errno to EINVAL and return -1.

备注Remarks

Fseek_fseeki64函数与关联的文件指针 (如果有) 的移动到的新位置偏移量从字节原点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. 自变量原点必须是中 STDIO 定义以下常量之一。H:The argument origin must be one of the following constants, defined in STDIO.H:

使用的原始值origin value 含义Meaning
SEEK_CURSEEK_CUR 文件指针的当前位置。Current position of file pointer.
SEEK_ENDSEEK_END 文件结尾。End of file.
SEEK_SETSEEK_SET 文件开头。Beginning of file.

你可以使用fseek_fseeki64指针重新定位任何位置的文件中。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_fseeki64清除文件尾指示器,并且不具备任何之前的效果ungetc调用针对fseek and _fseeki64 clears the end-of-file indicator and negates the effect of any prior ungetc calls against stream.

当文件打开以追加数据时,当前文件位置由最后的 I/O 操作确定,而不是由发生下一个写入的位置确定。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. 如果在为追加而打开的文件中尚未发生 I/O 操作,则文件位置是文件开头。If no I/O operation has yet occurred on a file opened for appending, the file position is the start of the file.

在文本模式下打开的流的fseek_fseeki64用途相当有限,因为回车换行符翻译可能会导致fseek_fseeki64以产生意外的结果。For streams opened in text mode, fseek and _fseeki64 have limited use, because carriage return-linefeed translations can cause fseek and _fseeki64 to produce unexpected results. 唯一fseek_fseeki64保证在文本模式下打开的流处理的操作:The only fseek and _fseeki64 operations guaranteed to work on streams opened in text mode are:

  • 使用相对于任何原始值的偏移 0 进行查找。Seeking with an offset of 0 relative to any of the origin values.

  • 查找从开始处的偏移量值的文件返回到调用ftell时使用fseek_ftelli64时使用 _fseeki64.Seeking from the beginning of the file with an offset value returned from a call to ftell when using fseek or _ftelli64 when using _fseeki64.

此外,在文本模式中,CTRL+Z 将在输入时解释为文件结尾字符。Also in text mode, CTRL+Z is interpreted as an end-of-file character on input. 打开以进行读取/写入的文件中fopen和所有相关的例程检查文件末尾的 CTRL + Z,如果可能删除它。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. 这样做是因为使用的组合fseekftell_fseeki64_ftelli64,将移动的文件中以结尾CTRL + Z 可能会导致fseek_fseeki64文件末尾附近运行不当。This is done because using the combination of fseek and ftell or _fseeki64 and _ftelli64, to move within a file that ends with a CTRL+Z may cause fseek or _fseeki64 to behave improperly near the end of the file.

当 CRT 打开以字节顺序标记 (BOM) 开头的文件时,文件指针位于 BOM 后面(即,位于文件实际内容的开头)。When the CRT opens a file that begins with a Byte Order Mark (BOM), the file pointer is positioned after the BOM (that is, at the start of the file's actual content). 如果你有到fseek到文件的开头,使用ftell来获取初始位置和fseek到它而不是位置 0。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_nolockFor a non-locking version, see _fseek_nolock, _fseeki64_nolock.

要求Requirements

函数Function 必需的标头Required header
fseekfseek <stdio.h><stdio.h>
_fseeki64_fseeki64 <stdio.h><stdio.h>

有关其他兼容性信息,请参阅 兼容性For additional compatibility information, see Compatibility.

示例Example

// 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 );
}
File pointer is set to middle of first line.
This is the file 'fseek.out'.

请参阅See also

流 I/OStream I/O
fopen、_wfopen_wfopenfopen, _wfopen
ftell、_ftelli64ftell, _ftelli64
_lseek、_lseeki64_lseek, _lseeki64
rewindrewind