ftell、_ftelli64ftell, _ftelli64

获取文件指针的当前位置。Gets the current position of a file pointer.

语法Syntax

long ftell(
   FILE *stream
);
__int64 _ftelli64(
   FILE *stream
);

参数Parameters

streamstream
目标文件结构。Target FILE structure.

返回值Return Value

ftell_ftelli64返回当前文件位置。ftell and _ftelli64 return the current file position. Ftell_ftelli64返回的值可能不会反映在文本模式下打开的流的物理字节偏移量,因为文本模式会导致回车换行符转换。The value returned by ftell and _ftelli64 may not reflect the physical byte offset for streams opened in text mode, because text mode causes carriage return-line feed translation. 结合使用ftellfseek_ftelli64_fseeki64 ,以正确地返回到文件位置。Use ftell with fseek or _ftelli64 with _fseeki64 to return to file locations correctly. 出现错误时, ftell_ftelli64将调用无效参数处理程序,如参数验证中所述。On error, ftell and _ftelli64 invoke the invalid parameter handler, as described in Parameter Validation. 如果允许执行继续, 则这些函数将返回-1L, 并将errno设置为 errno 中定义的两个常量之一。高.If execution is allowed to continue, these functions return -1L and set errno to one of two constants, defined in ERRNO.H. Ebadf ( 常量表示参数不是有效的文件指针值, 或者未引用打开的文件。The EBADF constant means the stream argument is not a valid file pointer value or does not refer to an open file. EINVAL表示传递给函数的参数无效。EINVAL means an invalid stream argument was passed to the function. 在不能查找的设备上 (例如终端和打印机), 或当stream未引用打开的文件时, 返回值为 undefined。On devices incapable of seeking (such as terminals and printers), or when stream does not refer to an open file, the return value is undefined.

有关这些代码以及其他返回代码的详细信息,请参阅 _doserrno、errno、_sys_errlist 和 _sys_nerrSee _doserrno, errno, _sys_errlist, and _sys_nerr for more information on these, and other, return codes.

备注Remarks

Ftell_ftelli64函数检索与stream关联的文件指针(如果有)的当前位置。The ftell and _ftelli64 functions retrieve the current position of the file pointer (if any) associated with stream. 位置表示为相对于流开头的偏移量。The position is expressed as an offset relative to the beginning of the stream.

请注意,当文件打开以追加数据时,当前文件位置由最后的 I/O 操作确定,而不是由发生下一个写入位置确定。Note that 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. 例如,如果打开文件进行追加,并且最后一次操作是读取,则文件位置将是下一个读取操作的开始点,而不是下一个写入的开始点。For example, if a file is opened for an append and the last operation was a read, the file position is the point where the next read operation would start, not where the next write would start. (在打开文件进行追加时,文件位置会在执行任何写操作前移动到文件的末尾。)如果在为进行追加而打开的文件中尚未发生任何 I/O 操作,则文件位置是文件的开头。(When a file is opened for appending, the file position is moved to end of file before any write operation.) If no I/O operation has yet occurred on a file opened for appending, the file position is the beginning of the file.

在文本模式中,CTRL+Z 将在输入时解释为文件尾字符。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. 完成此操作的原因是,使用ftellfseek_ftelli64_Fseeki64的组合在以 CTRL + Z 结尾的文件中移动可能导致ftell_ftelli64在靠近文件.This is done because using the combination of ftell and fseek or _ftelli64 and _fseeki64, to move within a file that ends with a CTRL+Z may cause ftell or _ftelli64 to behave improperly near the end of the file.

此函数在执行期间会锁定调用线程,因此是线程安全的。This function locks the calling thread during execution and is therefore thread-safe. 有关非锁定版本,请参阅 _ftell_nolockFor a non-locking version, see _ftell_nolock.

要求Requirements

函数Function 必需的标头Required header 可选标头Optional headers
ftellftell <stdio.h><stdio.h> <errno.h><errno.h>
_ftelli64_ftelli64 <stdio.h><stdio.h> <errno.h><errno.h>

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

示例Example

// crt_ftell.c
// This program opens a file named CRT_FTELL.C
// for reading and tries to read 100 characters. It
// then uses ftell to determine the position of the
// file pointer and displays this position.

#include <stdio.h>

FILE *stream;

int main( void )
{
   long position;
   char list[100];
   if( fopen_s( &stream, "crt_ftell.c", "rb" ) == 0 )
   {
      // Move the pointer by reading data:
      fread( list, sizeof( char ), 100, stream );
      // Get position after read:
      position = ftell( stream );
      printf( "Position after trying to read 100 bytes: %ld\n",
              position );
      fclose( stream );
   }
}
Position after trying to read 100 bytes: 100

请参阅See also

流 I/OStream I/O
fopen、_wfopen_wfopenfopen, _wfopen
fgetposfgetpos
fseek、_fseeki64fseek, _fseeki64
_lseek、_lseeki64_lseek, _lseeki64