ftell、_ftelli64ftell, _ftelli64

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

语法Syntax

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

参数Parameters

stream
目标文件结构。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-linefeed 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 中定义的两个常量之一。H。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. 无法查找 (例如终端和打印机) 的设备上时,或者当未引用打开的文件,则返回值不确定。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函数将检索与关联的文件指针 (如果有) 的当前位置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