fgetposfgetpos

获取流的文件位置指示器。Gets a stream's file-position indicator.

语法Syntax

int fgetpos(
   FILE *stream,
   fpos_t *pos
);

参数Parameters

streamstream
目标流。Target stream.

位置pos
位置指示器存储。Position-indicator storage.

返回值Return Value

如果成功, fgetpos将返回0。If successful, fgetpos returns 0. 如果失败,它将返回一个非零值,并将errno设置为以下清单常量之一(在 stdio.h 中定义)。H):Ebadf ( ,这意味着指定的流不是有效的文件指针或不可访问,也可能是EINVAL,这意味着值或pos的值无效,例如,如果这两个值都为 null 指针。On failure, it returns a nonzero value and sets errno to one of the following manifest constants (defined in STDIO.H): EBADF, which means the specified stream is not a valid file pointer or is not accessible, or EINVAL, which means the stream value or the value of pos is invalid, such as if either is a null pointer. 如果streampos指针,则函数将调用无效参数处理程序,如参数验证中所述。If stream or pos is a NULL pointer, the function invokes the invalid parameter handler, as described in Parameter Validation.

备注Remarks

Fgetpos函数获取参数的文件位置指示器的当前值,并将其存储在pos所指向的对象中。Fsetpos函数稍后可以使用存储在pos中的信息来重置参数的指针,使其在调用fgetpos时重置为其位置。The fgetpos function gets the current value of the stream argument's file-position indicator and stores it in the object pointed to by pos. The fsetpos function can later use information stored in pos to reset the stream argument's pointer to its position at the time fgetpos was called. Pos值以内部格式存储,仅供fgetposfsetpos使用。The pos value is stored in an internal format and is intended for use only by fgetpos and fsetpos.

要求Requirements

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

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

示例Example

// crt_fgetpos.c
// This program uses fgetpos and fsetpos to
// return to a location in a file.

#include <stdio.h>

int main( void )
{
   FILE   *stream;
   fpos_t pos;
   char   buffer[20];

   if( fopen_s( &stream, "crt_fgetpos.txt", "rb" ) ) {
      perror( "Trouble opening file" );
      return -1;
   }

   // Read some data and then save the position.
   fread( buffer, sizeof( char ), 8, stream );
   if( fgetpos( stream, &pos ) != 0 ) {
      perror( "fgetpos error" );
      return -1;
   }

   fread( buffer, sizeof( char ), 13, stream );
   printf( "after fgetpos: %.13s\n", buffer );

   // Restore to old position and read data
   if( fsetpos( stream, &pos ) != 0 ) {
      perror( "fsetpos error" );
      return -1;
   }

   fread( buffer, sizeof( char ), 13, stream );
   printf( "after fsetpos: %.13s\n", buffer );
   fclose( stream );
}

输入:crt_fgetpos.txtInput: crt_fgetpos.txt

fgetpos gets a stream's file-position indicator.

输出:crt_fgetpos.txtOutput crt_fgetpos.txt

after fgetpos: gets a stream
after fsetpos: gets a stream

请参阅See also

流 I/OStream I/O
fsetposfsetpos